setAttribute、onClick 和跨浏览器兼容性
我读过很多关于这个问题的帖子,但没有一个有任何可靠的答案。 这是我的代码:
// 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 注释)在这两种情况下都不起作用,我使用它是否不正确?
无论哪种方式,我都找不到在 IE 中实现此功能的方法,更不用说在两者中了。 我在使用 .onclick 方法时确实更改了函数调用,并且仅使用对警报函数的简单调用就可以正常工作,这就是为什么我认为我的语法不正确。
长话短说,我无法让 onclick 参数在 IE/Mozilla 之间一致工作。
——尼古拉斯
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);
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?
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.
Long story short, I can't get the onclick parameter to work consistently between IE/Mozilla.
-- Nicholas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
切勿在 HTML 文档上使用 setAttribute。 IE 在很多情况下都会犯严重错误,而 DOM-HTML 属性更短、更快、更容易阅读:
什么是“alines”? 为什么要将其转换为字符串并用单引号引起来? 看起来您正在尝试做一些令人发指的事情,涉及评估字符串中的代码(这就是您在下面的“onew.setAttribute”版本中所做的事情)。 在字符串中评估 JavaScript 代码几乎总是错误的; 像躲避瘟疫一样躲避它。 在上述情况下,IE 应该执行与 Firefox 相同的操作:它不应该工作。
如果 'alines[i]' 是一个字符串,我猜你想要做的是通过构造一个代码字符串来让它记住该字符串,该代码字符串将在 JavaScript 中计算为原始字符串。 但是:
还不够。 如果 'alines[i]' 中有撇号或反斜杠,会发生什么?
你有一个语法错误和可能的安全漏洞。 现在,您可以做一些费力且烦人的事情,例如:
尝试转义字符串,但它很丑陋,并且不适用于其他数据类型。 您甚至可以要求 JavaScript 为您做这件事:
但并非所有对象都可以转换为可计算的 JavaScript 源字符串; 基本上整个方法注定会失败。
如果您只想让 onclick 回调调用带有参数的函数,那么通常要做的就是以简单的方式编写代码:
通常这会起作用并且正是您想要的。 然而,您可能在这里遇到了一个轻微的问题,这可能会让您困惑于考虑古怪的弦乐方法。
也就是说,如果在这种情况下“i”是封闭“for”循环的变量,则对“alines[i]”的引用将不会执行您认为的操作。 当点击发生时,回调函数将访问“i”——即循环完成之后。 此时,“i”变量将保留循环结束时的任何值,因此“alines[i]”将始终是“alines”的最后一个元素,无论单击哪个“onew”。
(请参阅如何修复 ActionScript 3 (AS3) 中的关闭问题,了解对此的一些讨论。这是导致问题的最大原因之一JavaScript 和 Python 中的闭包混淆了,有一天真的应该在语言级别上修复。)
您可以通过将闭包封装在其自己的函数中来解决循环问题,如下所示:
在更高版本的 JavaScript 中,您可以简单地说:
如果您希望今天能够在浏览器中使用“Function.bind()”,您可以从 原型框架,或者直接使用:
Never use setAttribute on HTML documents. IE gets it badly wrong in many cases, and the DOM-HTML properties are shorter, faster and easier to read:
What is ‘alines’? Why are you converting it to a string and surrounding it with single quotes? It looks like you are trying to do something heinous involving evaluating code in a string (which is what you're doing below in the ‘onew.setAttribute’ version). Evaluating JavaScript code in strings is almost always the Wrong Thing; avoid it like the plague. In the above case, IE should do the same as Firefox: it shouldn't work.
If ‘alines[i]’ is a string, I guess what you're trying to do is make it remember that string by constructing a code string that will evaluate in JavaScript to the original string. But:
is insufficient. What happens if ‘alines[i]’ has an apostrophe in, or a backslash?
you've got a syntax error and possible security hole. Now, you could do something laborious and annoying like:
to try to escape the string, but it's ugly and won't work for other datatypes. You could even ask JavaScript to do it for you:
But not all objects can even be converted to evaluatable JavaScript source strings; basically the entire approach is doomed to failure.
The normal thing to do if you just want to have the onclick callback call a function with a parameter is to write the code in the straightforward way:
Generally this will work and is what you want. There is, however, a slight wrinkle which you may have hit here, which could be what is confusing you into considering the wacky approach with the strings.
Namely, if ‘i’ in this case is the variable of an enclosing ‘for’ loop, the reference to ‘alines[i]’ won't do what you think it does. The ‘i’ will be accessed by the callback function when the click happens — which is after the loop has finished. At this point the ‘i’ variable will be left with whatever value it had at the end of the loop, so ‘alines[i]’ will always be the last element of ‘alines’, regardless of which ‘onew’ was clicked.
(See eg. How to fix closure problem in ActionScript 3 (AS3) for some discussion of this. It's one of the biggest causes of confusion with closures in both JavaScript and Python, and should really be fixed at a language level some day.)
You can get around the loop problem by encapsulating the closure in its own function, like this:
And in a later version of JavaScript, you'll be able to say simply:
If you would like to be able to use ‘Function.bind()’ in browsers today, you can get an implementation from the Prototype framework, or just use:
我通常使用类似的东西:
这应该在 IE e Firefox 中都有效。
I usually use something like:
this should work both in IE e Firefox.
将
addEventListener()
函数与“<对于基于 Mozilla 的浏览器,click
”为type
参数,并且attachEvent()
函数,以“onclick
”作为sEvent
参数对于IE; 我发现最好使用 try/catch 语句,例如:Use the
addEventListener()
function with "click
" for thetype
argument for Mozilla-based browsers, andattachEvent()
function with "onclick
" as thesEvent
argument for IE; I find it best to use a try/catch statement, for example:我认为#3抗议太多了。 在很多情况下,我动态构建表并需要将参数传递给回调函数。 这不是类型安全问题,因为我的变量 parm 是相关表行的整数索引。 这是具有可变参数和固定参数的代码,似乎是跨浏览器兼容的:
I think #3 protesteth too much. In a lot of situations I'm building a table dynamically and need to pass parameters to the callback function. It isn't a typesafe issue since my variable parm is an integer index to the table row in question. Here's code with both a variable and fixed parameter that seems to be cross-browser compliant: