读取html对象并用读取值替换javascript属性
我有一个JavaScript参数对象,其中包含以下结构中的一些HTML内容读取html对象并用读取值替换javascript属性,javascript,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-p
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>
我想从HTMLdiv中读出数据参数,并将它们分配给JavaScript函数中的相同命名函数
所有这些都应该发生在JavaScript函数的顶部,并替换同一函数中的属性
感谢您提供的帮助或如何实现这一点的建议,我对JavaScript不是很熟练。假设jQuery: 在html中创建一个不可见的div 设置div的innerHTML 将不可见div的属性设置为对象: $invisible.htmlcontent; 选项[data onclick]=$invisible.findcontent.attrdata-onclick; 选项[data progress]=$invisible.findcontent.attrdata-progress;
// 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" />