setAttribute、onClick 和跨浏览器兼容性 [英] setAttribute, onClick and cross browser compatibility

查看:36
本文介绍了setAttribute、onClick 和跨浏览器兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于这个的帖子,但没有一个有任何可靠的答案.这是我的代码:

I have read a number of posts about this but none with any solid answer. Here is my code:

// button creation
onew = document.createElement('input');
onew.setAttribute("type", "button");
onew.setAttribute("value", "hosts");
onew.onclick = function(){fnDisplay_Computers("'" + alines[i] + "'"); }; // ie
onew.setAttribute("onclick", "fnDisplay_Computers('" + alines[i] + "')"); // mozilla
odiv.appendChild(onew);

现在,setAttribute() 方法(带有 mozilla 注释)在 mozilla 中工作正常,但前提是它出现在它上面的行之后.所以换句话说,它似乎只是默认为最后设置的那个..onclick 方法(带有 ie 注释)在任何一种情况下都不起作用,是我使用不当吗?

Now, the setAttribute() method (with the mozilla comment) works fine in mozilla but only if it comes AFTER the line above it. So in other words it seems to just default to whichever gets set last. The .onclick method (with the ie comment) does not work in either case, am I using it incorrectly?

无论哪种方式,我都无法在 IE 中找到一种方法来完成这项工作,更不用说两者了.我确实在使用 .onclick 方法时更改了函数调用,并且仅使用对警报函数的简单调用就可以正常工作,这就是为什么我认为我的语法不正确.

Either way I can't find a way to make this work at all in IE, let alone in both. I did change the function call when using the .onclick method and it worked fine using just a simple call to an alert function which is why I believe my syntax is incorrect.

长话短说,我无法让 onclick 参数在 IE/Mozilla 之间保持一致.

Long story short, I can't get the onclick parameter to work consistently between IE/Mozilla.

-- 尼古拉斯

推荐答案

我通常使用类似的东西:

I usually use something like:

onew.onclick = new Function("fnDisplay_Computers('" + alines[i] + "')");

这应该适用于 IE e Firefox.

this should work both in IE e Firefox.

这篇关于setAttribute、onClick 和跨浏览器兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆