javascript - dom元素可见性对javascript很重要吗
问题描述
我知道id
对于当前的 dom 必须是唯一的,但是即使 2 个 dom 元素永远不会同时可见,这是否成立。
我有这种情况:
<div class="visible-lg visible-md">
<div id="test"></div>
</div>
.
.
.
<div class="visible-sm visible-xs">
<div id="test"></div>
</div>
<script>
.
.
.
document.getElementById("test").innerHTML = "this text";
</script>
无论我使用什么设备尺寸,只有第一个元素会得到更新。如何确保正确的元素得到更新?
解决方案
I know id has to be unique for the current dom, but does that hold even if the 2 dom elements will never be visible at the same time.
Yes. Visibility is irrelevant, id
s must be unique. It's not a JavaScript thing, it's a DOM thing [the layer under HTML]:
An element can have an associated unique identifier (ID)
Note the term "unique."
推荐阅读
- excel - MS Excel TRUE/FALSE 公式未返回预期结果
- docker - 运行“ansible-container build”时出错
- python - 从 CSV 文件加载虹膜数据集?
- html - SmoothScroll、Safari 支持和响应式导航
- c# - InstaSharper 令牌
- asp.net-core - 如何在 ASP .NET Core 的 Area 中使用 css 文件或 js
- java - 计算给定整数元素数组中范围 * {0, ..., r} 中整数的出现次数
- ansible - 在远程节点上运行远程脚本
- nginx - nginx服务器默认使用HTTP/2协议版本?
- google-bigquery - BigQuery 中复杂的字符串聚合