Home  >  Article  >  Backend Development  >  How to dynamically modify the title of a web page in php

How to dynamically modify the title of a web page in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-29 14:48:174313browse

How to dynamically modify the title of a web page in php: 1. Use js method, the code is [document.title = 'hello world!']; 2. Use jQuery method, the code is [

How to dynamically modify the title of a web page in php

#php method to dynamically modify the title of a web page:

1 , js way

First, I thought of using document.getElementsByTagName() to get the title tag of the page, which can be obtained. For example:

<title>标题</title>
<script>
    var Title = document.getElementsByTagName(&#39;title&#39;)
    console.log(Title)  // <title>标题</title>
</script>

However, When I want to use Title.title to get or set the value, it does not take effect.

Get the title value: console.log(Title.title) You will find that the value cannot be obtained

Similarly, set the title value of the web page: Title.title = 'hello world!' You will find that the title of the web page has not been changed

So through this method It is impossible to obtain and change the title of the web page.

In fact, we can directly obtain the title value or set the title value through document.title

<title>标题</title>
<script>
    document.title = &#39;hello world!&#39;
</script>

Result:

How to dynamically modify the title of a web page in php  

【Related learning recommendation: js video tutorial

It can be found that this method is feasible.

2. jQuery method

<title>标题</title>
<script src="https://www.php.cn/jquery/3.2.1/jquery.js"></script>
<script>
    $(function(){
      $(&#39;title&#39;).html(&#39;hello!&#39;) // 此处也可以使用text()方法
    })
</script>

Relevant learning recommendations: php programming (video), jQuery video tutorial

The above is the detailed content of How to dynamically modify the title of a web page in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn