我是HTML新手,特别是HTML5。
我尝试选择ID为mr
的标头,并将其更改为para
作为其类别的项目数的长度。
我的浏览器视图中似乎没有任何变化。
不确定原因。感谢。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
var items = document.getElementsByClassName("para");
var log = document.getElementById("mr");
log.innerText = items.length;
</script>
<title>Title</title>
<html lang="en"/>
</head>
<body>
<header>
<h1 id="mr">This is a header</h1>
</header>
<nav>
<a href="index.html">Index</a>
</nav>
<section>
<h1>Section 1</h1>
<article>
<p class="para">here is bla lakjfbsjkafb asje bfjlsaeb jlfkaseb ljkfabejkf bajekbf</p>
</article>
<article>
<p class="para"> <mark>here is some more shit</mark></p>
</article>
</section>
<footer>
</footer>
</body>
</html>
答案 0 :(得分:2)
脚本运行时,DOM和mr都不存在。
选项1
只需将script
部分放在关闭正文标记
<script>
var items = document.getElementsByClassName("para");
var log = document.getElementById("mr");
log.innerText = items.length;
</script>
</body>
选项2
如果您仍想将其放入head
标记内,请将js放入window.onload
<script>
window.onload=function(){
var items = document.getElementsByClassName("para");
var log = document.getElementById("mr");
log.innerText = items.length;
}
</script>
答案 1 :(得分:0)
你必须记住关于HTML的事情是它从上到下阅读和解析。意味着顶部的东西首先被渲染,而后面的东西则被渲染。将JS放在正文末尾或将其推迟到页面加载通常是一种很好的做法。
<body>
<h1 id="mr">This is a header</h1>
<p class="para">here is bla lakjfbsjkafb asje bfjlsaeb jlfkaseb ljkfabejkf bajekbf</p>
<p class="para">
<mark>here is some more stuff</mark>
</p>
<script>
var items = document.getElementsByClassName("para");
var log = document.getElementById("mr");
log.innerText = items.length;
</script>
</body>
</html>
&#13;
答案 2 :(得分:0)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script>
function startup() {
var items = document.getElementsByClassName("para");
var log = document.getElementById("mr");
log.innerText = items.length;
}
</script>
<title>Title</title>
<html lang="en"/>
</head>
<body onload="startup();">
<header>
<h1 id="mr">This is a header</h1>
</header>
<nav>
<a href="index.html">Index</a>
</nav>
<section>
<h1>Section 1</h1>
<article>
<p class="para">here is bla lakjfbsjkafb asje bfjlsaeb jlfkaseb ljkfabejkf bajekbf</p>
</article>
<article>
<p class="para"> <mark>here is some more shit</mark></p>
</article>
</section>
<footer>
</footer>
</body>
</html>
脚本在页面加载之前运行,为了解决这个问题,我们在主体加载后运行代码。
答案 3 :(得分:0)
将您的js代码放在document.onload事件中的scripts标记中,因为在此事件之后将确保在触发此事件时,DOM元素已加载并准备好由DOM API访问。
<script>
document.onload = function(){
var items = document.getElementsByClassName("para");
var log = document.getElementById("mr");
log.innerText = items.length;
};
</script>
答案 4 :(得分:0)
首先,您的脚本应该在最后帮助加载用户首先看到的内容更快,并且确保所有DOM元素都被加载。第二个我会用log.innerText
替换log.innerHTML
,因为不知何故log.innerText
对我来说真的不起作用,我更喜欢log.innerHTML
,因为你可以使用html标签和东西。
所以这是一个至少适用于我的脚本:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<html lang="en" />
</head>
<body>
<header>
<h1 id="mr">This is a header</h1>
</header>
<nav>
<a href="index.html">Index</a>
</nav>
<section>
<h1>Section 1</h1>
<article>
<p class="para">here is bla lakjfbsjkafb asje bfjlsaeb jlfkaseb ljkfabejkf bajekbf</p>
</article>
<article>
<p class="para">
<mark>here is some more stuff</mark>
</p>
</article>
</section>
<footer>
</footer>
<script>
var items = document.getElementsByClassName("para");
var log = document.getElementById("mr");
log.innerHTML = items.length;
</script>
</body>
</html>
&#13;