ie和火狐兼容问题 js代码如下:document.getElementById(“id”).click()在ie中可行,但是在火狐中不行,请问要怎么写才可以都兼容。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"> <script src="js/jquery-1.3.2.js"></script><script type="text/javascript">window.onload=function(){ if(document.all) document.getElementById("pad").click(); else document.getElementById("pad").onclick();}</script></head><body> <div id="pad" onclick="alert('ok');"></div> </body></html> 不好意思,火狐下不行换一个if(document.all) { document.getElementById(elmid).click(); } else { var evt = document.createEvent("MouseEvents"); evt.initEvent("click", true, true); document.getElementById(elmid).dispatchEvent(evt); } <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><HTML><head><script>if(!HTMLElement.click) HTMLElement.prototype.click = function(){ this.onclick(); } function se(){alert('a');}function haha(){ document.getElementById('ss').click();}</script></head><BODY><div id='ss' style="height:100px; width:100px; background-color:#000000" onClick="se()"> </div><input value="额" type="button" onClick="haha()"></BODY></HTML> click不是每个元素都有的,而且getElementById可能返回null 用jquery试试:$("#id").bind("click",function (){......});应该就可以完全兼容了。 参见:initEvent,dispatchEvent javascript用ActiveXObject时IE弹出“安全警告”,如何去掉 关于jquery的thickbox插件 急,请资深专家进,javascript节点空格问题? 【散分贴】谢谢这一年来帮助过的朋友们 100分,谁可以实现这个效时 我参加福建省第二届软件技术大赛的获奖作品 在线等 这个关于正则的脚本错在哪 为什么会提示“对象不支持此属性或方法” 怎样得到用户是 用F5这键,ctrl+R,还是用菜单栏的 刷新 按钮 来进行刷新的呢? 请问如何编下拉菜单? 有关Activex 只下载一次地问题 怎么实现一个基于网页的视频服务器?请介绍大体技术
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<script src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
window.onload=function(){
if(document.all) document.getElementById("pad").click();
else document.getElementById("pad").onclick();
}
</script>
</head>
<body>
<div id="pad" onclick="alert('ok');"></div>
</body>
</html>
不好意思,火狐下不行换一个if(document.all)
{
document.getElementById(elmid).click();
}
else
{
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
document.getElementById(elmid).dispatchEvent(evt);
}
<HTML>
<head>
<script>
if(!HTMLElement.click)
HTMLElement.prototype.click = function(){
this.onclick();
}
function se(){
alert('a');
}function haha(){
document.getElementById('ss').click();
}
</script>
</head>
<BODY>
<div id='ss' style="height:100px; width:100px; background-color:#000000" onClick="se()"> </div>
<input value="额" type="button" onClick="haha()">
</BODY>
</HTML>
$("#id").bind("click",function (){......});
应该就可以完全兼容了。
参见:initEvent,dispatchEvent