<body class="div_gray" id="body" onkeydown="index_enter()"> <div id="page" style="display: block;" > <div style="margin:300px 0 0 0;"> <div class="W_index_transparentK" style="display:block;"> <table cellpadding="0" cellspacing="0" class="W_index_tableK" style="position:static;width:556px;margin:0 auto;"><!-- style="width:556px;left:0;"> --> <tr> <td class="W_index_transparentK_td0"> <span class="W_index_tra_input"><img src="css/images/yonghu.png" alt="" class="W_index_tra_input_wel" /><input type="text"value="账号" class="W_index_tra_input_txt" id="W_phonenum"/></span> <span id="W_index_leftspan" class="W_index_tra_input" style="margin-left: 15px;"><img src="css/images/phone.png" alt="" class="W_index_tra_input_wel" /><input type="password" value="" style="display:none;" class="W_index_tra_input_txt" id="W_Authvalue"/><input type="text"value="登录密码" class="W_index_tra_input_txt" id="W_Authvalue_show"/></span> </td> </tr> </table> </div> </div> <div class="W_index_landing" id="Enter" onclick="landing_w()"> 登 录 </div>
以上是页面代码。
使用JS触发DIV的onclick事件无效,请问各位大神如何触发DIV登陆的onclick事件呢?
document.getElementById("Enter").click();
参考Programatically fire crossbrowser click event with JavaScript:
function fireClick(node){ if (document.createEvent) { var evt = document.createEvent('MouseEvents'); evt.initEvent('click', true, false); node.dispatchEvent(evt); } else if(document.createEventObject) { node.fireEvent('onclick') ; } else if (typeof node.onclick == 'function') { node.onclick(); } } fireClick(theNode);
function fireClick(node){ if (document.createEvent) { var evt = document.createEvent('MouseEvents'); evt.initEvent('click', true, false); node.dispatchEvent(evt); } else if(document.createEventObject) { node.fireEvent('onclick') ; } else if (typeof node.onclick == 'function') { node.onclick(); } } var theNote = document.getElementById('Enter'); fireClick(theNode);
还是不行
function fireClick(node){ if (document.createEvent) { var evt = document.createEvent('MouseEvents'); evt.initEvent('click', true, false); node.dispatchEvent(evt); } else if(document.createEventObject) { node.fireEvent('onclick') ; } else if (typeof node.onclick == 'function') { node.onclick(); } } var theNode = document.getElementById('Enter'); fireClick(theNode); 还是不行, 有什么方法可以测试究竟 document.getElementById('Enter') 获取它有没有效啊。
@OurCrazy: fireClick(theNode);是放在哪执行的?
写一个Js
wwindow.onload=funtion(){
var div = document.getElementById('div1');
div.onclick=funtion(){
执行内容
}
}