用ID替换document.getElementById(“ id”)

发布于 2025-01-29 04:01:29 字数 538 浏览 2 评论 0原文

看来此代码运行良好。请在回答之前先尝试一下:

    <!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"></p>
<script>
let x = 7;
let y = 45;
let z = x + y;
demo.innerHTML = "The value of z is: " + z;//please note this line
</script>
</body>
</html>

这是否意味着我可以替换document.getElementById(“ id”)。只有ID。???

It seems that this code is working well. Please try it out before you answer:

    <!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"></p>
<script>
let x = 7;
let y = 45;
let z = x + y;
demo.innerHTML = "The value of z is: " + z;//please note this line
</script>
</body>
</html>

Does this mean that I can replace document.getElementById("id"). with just id.???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房敞 2025-02-05 04:01:29

它正在工作,但被认为是遗产和不安全。

It's working but it's considered legacy and unsafe.

禾厶谷欠 2025-02-05 04:01:29

看起来确实如此,并且也适用于嵌套元素...我想知道它是否在从HTML导入的其他JS文件上起作用。

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables.</p>
<p id="demo1">
  <p id="demo2"></p>
</p>
<p id="demo"></p>
<script>
let x = 7;
let y = 45;
let z = x + y;
demo.innerHTML = "The value of z is: " + z;//please note this line

demo2.innerHTML = "The value of z is: " + z;//please note this line
console.log(demo);
</script>
</body>
</html>

Seems like it does, and it works for nested elements also... I wonder though if it works on a different js file imported from the html.

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables.</p>
<p id="demo1">
  <p id="demo2"></p>
</p>
<p id="demo"></p>
<script>
let x = 7;
let y = 45;
let z = x + y;
demo.innerHTML = "The value of z is: " + z;//please note this line

demo2.innerHTML = "The value of z is: " + z;//please note this line
console.log(demo);
</script>
</body>
</html>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文