Home > Web Front-end > JS Tutorial > body text

How to set html title in js

青灯夜游
Release: 2021-12-13 14:02:32
Original
5379 people have browsed it

Method: 1. Use the "document.getElementsByTagName("title")[0].innerText='value'" statement; 2. Use the "document.title='value'" statement; 3. Use " $('title').html('value')" statement.

How to set html title in js

The operating environment of this tutorial: windows7 system, javascript1.8.5&&jquery1.10.2 version, Dell G3 computer.

js sets the html title title

innerText method

Through console.log(document. getElementsByTagName("title")[0]), and found that the

label can be printed. There are only text nodes in the label, so I guess it can only recognize TextNode, so I used innerText to set the value of title, and it succeeded. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">document.getElementsByTagName("title")[0].innerText = &#39;需要设置的值&#39;;</pre><div class="contentsignin">Copy after login</div></div><p class="jb51code"><strong>document.title method</strong><br/></p><p>After testing, the value of title can also be set through document.title. </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false">console.log(document.title); # 可以获取title的值。 document.title = &#39;需要设置的值&#39;; # 设置title的值。</pre><div class="contentsignin">Copy after login</div></div><p><strong>jQuery method</strong></p><p>jQuery method We change the value of title when the browser gains focus and loses focus. You can find that when switching browser tabs, The title has changed. </p><p> Of course, if your project relies on jQuery, you can use the jq method to set it </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$(&#39;title&#39;).html(&#39;需要设置的值&#39;) $(&#39;title&#39;).text(&#39;需要设置的值&#39;)</pre><div class="contentsignin">Copy after login</div></div><p> Example: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false"><!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>hello</title> <script src="js/jquery-1.7.2.min.js"></script> </head> <body> <script type="text/javascript"> // $(&#39;title&#39;).html(&#39;php中文网&#39;); $(&#39;title&#39;).text(&#39;php中文网&#39;); </script> </body> </html></pre><div class="contentsignin">Copy after login</div></div><p><img src="https://img.php.cn/upload/image/616/738/796/1639375311415707.png" title="1639375311415707.png" alt="How to set html title in js"></p> <p><span style="font-size: 18px;"><strong>Summary</strong></span></p> <p>In native js, we can dynamically modify the title of the web page in two ways: <code>innerText</code> and <code>document.title</code>.</p> In <p>jq, we can modify it through <code>$('title').html('')</code> or <code>$('title').text('')</code>. </p> <p>[Recommended learning: <a href="https://www.php.cn/course/list/2.html" target="_blank">javascript advanced tutorial</a>]</p>

The above is the detailed content of How to set html title in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!