好吧,我的.HTML文件中包含以下内联JavaScript事件,我想将其JavaScript替代项放入.JS文件中。然后,我将.JS文件链接到.HTML文件。以下是事件:

oncontextmenu
onmousedown
onload
onclick
onerror
onmouseover
onmouseout

最佳答案

<div id = "yourElement"></div>
<script>
    var yourElement = document.getElementById("yourElement"); //get the element
    yourElement.oncontextmenu = function () {
        //oncontextmenu triggered!
    }
    yourElement.onmousedown = function () {
        //onmousedown triggered!
    }
    yourElement.onload = function () {
        //onload triggered!
    }
    //ecc..
</script>


编辑:
这是我的小提琴:http://jsfiddle.net/durZL/
注意:如果您将脚本放在标头中,则应该告诉javascript在页面完全加载时运行代码,否则您将无法获取该元素,jQuery已经为您执行了此操作,但是如果没有想要使用它,这是正确的方法:

window.onload = function () {
   //here is the code that will run after the page is fully loaded
   //DOM elements can be safely manipulated here
}

08-25 15:18