我想在同一个标签中获取一个标签的id,然后如果可能的话,将其作为参数发送到javascript。 像这样的东西是我现在尝试过的:
var entity = document.createElement("a");
entity.setAttribute("id", name);
entity.setAttribute("href", "javascript:playAudio('"+path+"', this.id)");
我希望它是动态的,这样如果 id 发生变化,javascript 调用就会使用新 id 完成。
在这种情况下使用EventListener
更合适:
var entity = document.createElement("a");
entity.setAttribute("id", name);
entity.addEventListener("click", function(e){
playAudio(path, this.id);
e.preventDefault();
});
你应该附加一个事件处理程序,而不是将JavaScript注入href
属性中
entity.setAttribute('href', '#');
entity.addEventListener('click', function() {
playAudio(path, this.id);
return false;
}, false);
将 javascript 粘贴在 onclick。 href 中的this
是指窗口而不是锚点。
var entity = document.createElement("a");
entity.setAttribute("id", name);
entity.setAttribute("onclick", "playAudio('"+path+"', this.id)");