javascript - 根据元素 ID 从数据部分获取值
问题描述
我想在页脚部分的 h2 中动态呈现部分 id="zero" 数据部分值。由于我所有的页面都有不同的数据部分值,我希望我们可以定位部分 id="zero"。这样我就可以在我的网站上只使用一个页脚。我只是不精通 javascript 或 jQuery 来正确分配和调用。我知道有 document.getElementById('zero') 但是从数据部分获取值并让它出现在我的 H2 中我不清楚。
<section id="zero" class="section-wrap" data-section="Page Name"></section>
<section id="footer">
<h2></h2>
</section>
解决方案
这是一些应该有所帮助的Javascript。
//Get the data-section value
let val = document.getElementById('zero').dataset.section;
//find the first h2 inside footer section
let header = document.getElementById('footer').getElementsByTagName("h2")[0];
//set the value
header.textContent = val;
推荐阅读
- java - 在 LG V30 上打开 QR 扫描仪意图时引发“访问被拒绝查找属性 'camera.hal1.packagelist'”错误?
- flutter - 颤动中的可点击水平列表视图。如何使用列表中的图像制作可点击的列表视图
- tensorflow - 如何在 Graphcore IPU 上实现模型并行性?
- javascript - 如何使用 Shallow 组件 Jest/Enzyme 对不同的测试场景进行不同的模拟实现?
- networking - Opendaylight Hello World RPC 抛出“无法找到匹配的构造函数”错误
- c - STM32F407 Disc E31 调试中的错误信息
- git - 使用 git commit 消息的一部分作为 git log 中的变量
- python - 特征检测(复制/移动)
- python - Python TypeError:得到了一个意外的关键字参数“名称”
- android - setDeveloperPayload 已弃用,有什么替代方法吗?