我想用javascript中的extern替换我的onclick,onmouseover和onmouseout属性,这些属性现在在我的html中内联。
HTML:
<div onclick:"myfunctiononclick()"></div>
<div onmouseover:"myfunctionmouseover()"></div>
<div onmouseout:"myfunctionmouseout()"></div>
的javascript:
function myfunctiononclick(){something};
function myfunctiononmouseover(){something};
function myfunctiononmouseout(){something};
为什么我要这样做?因为chrome不允许在扩展名中使用内联javascript。
由于
顺便说一下,请不要使用jQuery,所以我知道发生了什么。答案 0 :(得分:0)
以某种方式参考元素,然后addEventListener
:
document.addEventListener("DOMContentLoaded", function() {
//This code is ran when the DOM content has been loaded
//I.E. the elements we are trying to get exist on the page
var divs = document.querySelectorAll("div");
divs[0].addEventListener("click", myfunctiononclick, false);
divs[1].addEventListener("mouseover", myfunctiononmouseover, false);
divs[2].addEventListener("mouseout", myfunctiononmouseout, false);
}, false);