javascript - 读取 html 对象并用读取的值替换 javascript 属性
问题描述
我有一个 JavaScript 参数对象,其中包含以下结构中的一些 HTML 内容。
function (type, content, title) {
// Replace code should go here
options = {
"data-onclick": null;
"data-progress": false;
}
}
内容对象的内容如下所示:
<div id=content data-onclick="onclickValue" data-progress="true"></div>
我想data-
从 HTML div 中读出参数并分配它们以替换 JavaScript 函数中相同的命名函数。
所有这些都应该发生在我的 JavaScript 函数的顶部,并替换同一函数中的属性。
感谢您提供任何帮助或指示如何实现这一点,我对 JavaScript 不是很熟练。
解决方案
// first select the html element
var element = document.getElementById('content');
// use the getAttribute('attribute-to-get') method to get your attributes
var options = {
'data-onclick': element.getAttribute('data-onlick'),
'data-progress': element.getAttribute('data-progress')
};
// consider using a hidden input field rather than hiding the element with css
// this is best practice
<input type="hidden" id="content" data-onclick="onclickValue" data-progress="true" />
推荐阅读
- javascript - 损坏的图像图标和类型错误:comments.map 不是函数
- python-3.x - 图像上对象之间的距离
- javascript - 编辑器之前需要的制表符表上下文单击事件
- python - 如何编辑附加数据?
- python - 转换为熊猫日期时间格式时的数据框列导致 NaT
- firebase - 通过身份验证来限制对 Firebase 的访问是否更好?
- python-3.x - 如何在文本文件的字符串末尾添加换行符?
- java - Appium for iOS 需要很长时间才能在屏幕上找到显示视频广告的元素
- javascript - 使用 mapmyindia 的 Autosuggest API
- sql - 如何拉出日期值为空的行