我在尝试创建一个动态标题时遇到了错误。

huangapple go评论69阅读模式
英文:

I'm getting an error while trying to create a dynamic title

问题

<title id="title">我的网站</title>
var titles = ["我的网站", "很高兴见到你!😁", "祝你有美好的一天!🌞"];
var index = 0;

function changeTitle() {
    document.getElementById("title").innerText = titles[index];
    index = (index + 1) % titles.length;
}
setInterval(changeTitle, 7000);
英文:

I want to create a dynamic title for my website using HTML and JavaScript. However, I'm getting an error in the developer console that says 'TypeError: null is not an object (evaluating 'document.getElementById("send").addEventListener')'. I want to understand why it's not reading the objects in the JavaScript-defined array.

JS:

var titles = [&quot;My website&quot;, &quot;Glad to see you!&#128525;&quot;, &quot;Have a nice day!&#129395;&quot;];
 var index = 0;

 function changeTitle() {
    document.getElementById(&quot;title&quot;).innerText = titles[index];
    index = (index + 1) % titles.length;
  }
  setInterval(changeTitle, 7000);

HTML :
&lt;title id=&quot;title&quot;&gt;My website&lt;/title&gt;

答案1

得分: 2

你不应该在<body>标签内声明<title>。它是为文档的<head>部分保留的。它指定了网页的实际标签/窗口标题。

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
const customTitleEl = document.getElementById("title");
const titles = ["我的网站", "很高兴见到你!😊", "祝你有美好的一天!🌞"];
let currentIndex = 0;

function changeTitle() {
  customTitleEl.textContent = titles[currentIndex];
  currentIndex = (currentIndex + 1) % titles.length;
}
changeTitle(); // 初始调用
setInterval(changeTitle, 1000); // 每“秒”更新

<!-- 语言: lang-html -->
<!-- <title> 应该在文档的 <head> 部分中声明(只有一次!) -->
<!-- 在 <body> 中使用一个 <div> 替代 -->
<div id="title">我的网站</div>

<!-- 结束代码片段 -->

如果你实际上想要更新网页的标题,可以调用:

document.title = '我的新标题';
英文:

You should not declare a &lt;title&gt; inside the &lt;body&gt;. It is reserved for the document's &lt;head&gt;. It specifies the actual tab/window title for the webpage.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const customTitleEl = document.getElementById(&quot;title&quot;);
const titles = [&quot;My website&quot;, &quot;Glad to see you!&#128525;&quot;, &quot;Have a nice day!&#129395;&quot;];
let currentIndex = 0;

function changeTitle() {
  customTitleEl.textContent = titles[currentIndex];
  currentIndex = (currentIndex + 1) % titles.length;
}
changeTitle(); // Call it initially
setInterval(changeTitle, 1000); // Update every &quot;second&quot;

<!-- language: lang-html -->

&lt;!-- The &lt;title&gt; should be declared in the document &lt;head&gt; (only once!) --&gt;
&lt;!-- Use a &lt;div&gt; inside the &lt;body&gt; instead --&gt;
&lt;div id=&quot;title&quot;&gt;My website&lt;/div&gt;

<!-- end snippet -->

If you actually want to update the webpage's title, call:

document.title = &#39;My new title&#39;;

huangapple
  • 本文由 发表于 2023年7月12日 22:45:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定