onclick=“VS事件处理程序如果我希望执行一个函数,我更喜欢执行内联js:<p id="element" onclick="doSomething();">Click me</p>因为它更容易调试。但是,我听到人们说不要使用内联js,并这样做:document.getElementById('element').onclick = doSomething;为什么推荐js事件侦听器?
3 回答
神不在的星期二
TA贡献1963条经验 获得超6个赞
<p class="element" onclick="doSomething();">Click me</p><p class="element" onclick="doSomething();">Click me</p> <p class="element" onclick="doSomething();">Click me</p><p class="element" onclick="doSomething();">Click me</p> <p class="element" onclick="doSomething();">Click me</p><p class="element" onclick="doSomething();">Click me</p>
<p class="element">Click me</p><p class="element">Click me</p><p class="element">Click me</p><p class="element">Click me</p>
<p class="element">Click me</p><p class="element">Click me</p>$('.element').bind('click', doSomethingElse);编辑:
Project = {
// All the variables/constants/objects that need to be globally accessible inside the Project object.
init : function(){
// Main entry point...
this.MainMenu.init();
// Rest of the code which should execute the moment Project is initiated.
}}Project.MainMenu = {
// All the variables/constants/objects that need to be accessible only to MainMenu.
init : function(){ // Is run immediatelly by Project.init()
// Event handlers relevant to the main menu are bound here
// Rest of the initialization code
}}Project.SlideShow = {
// All the variables/constants/objects that need to be accessible only to SlideShow.
init : function(){ // Is run only on pages that really require it.
// Event handlers for the slideshow.
}}添加回答
举报
0/150
提交
取消
