jQuery attr() 在 IE7 中不返回
这个把我难住了。我在某些链接上使用“for”属性来标识它们应该作用的元素。在 Firefox、IE9 和 IE8 中,一切似乎都工作正常……但 IE7 会崩溃并返回“未定义”。
这是我正在使用的 jQuery 代码的要点:
$(document.ready(function() {
var editor_icons = $('.edit-button');
editor_icons.each(function() {
var $this = $(this),
parent = $('#' + $this.attr('for'));
var left = parent.position().left + parent.innerWidth() - 58 - 3,
top = parent.position().top + 3;
// ... you get the point ...
});
});
应该作用的示例 HTML 元素:
<div id="content_wrapper">
<a class="edit-button" href="javascript:void(0);" for="index_primary_content" />
<div id="index_primary_content">
....
</div>
</div>
在您指出之前,我意识到锚点不应该是自动关闭元素。我从应用程序发送的 HTML 是
,IE 将其解释为
。我在元素之间添加了一个
以确保这不是解释问题,但我得到了完全相同的错误。
问题是 $this .attr('for')
返回“未定义”,因此 parent.position().left
抛出“对象为 null 或未定义”错误。
我用观察变量进行了一些挖掘,我可以看到选择器正在工作,并且在此上下文中的 $this
确实选择了正确的元素并且确实设置了“for”属性......但是我认为 jQuery 没有找到它。
正如我所说,它在 Firefox、IE9 和 IE8 中运行得很好……只是在 IE7 中不行。有想法吗?
作为参考,我正在使用 jQuery 1.6.2 ...
This one has me stumped. I'm using the "for" attribute on some links to identify the element they're supposed to act on. Everything seems to be working just fine in Firefox, IE9, and IE8 ... but IE7 breaks and returns "undefined."
Here's the gist of the jQuery code I'm using:
$(document.ready(function() {
var editor_icons = $('.edit-button');
editor_icons.each(function() {
var $this = $(this),
parent = $('#' + $this.attr('for'));
var left = parent.position().left + parent.innerWidth() - 58 - 3,
top = parent.position().top + 3;
// ... you get the point ...
});
});
An example HTML element this should be acting on:
<div id="content_wrapper">
<a class="edit-button" href="javascript:void(0);" for="index_primary_content" />
<div id="index_primary_content">
....
</div>
</div>
Before you point it out, I realize anchors aren't supposed to be auto-closing elements. The HTML I'm sending from my application is
<a></a>
and IE is interpreting it as<a />
. I added ain between the elements to make sure it wasn't an interpretation issue, and I get the exact same error.
The problem is that $this.attr('for')
returns "undefined", so parent.position().left
throws an "object is null or undefined" error.
I dug down with watch variables a bit, and I can see that the selectors are working, and $this
in this context does select the right elements and does have the "for" attribute set ... but jQuery isn't finding it I think.
As I said, it works just fine in Firefox, IE9, and IE8 ... just not IE7. Ideas?
For reference, I'm using jQuery 1.6.2 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这是您的问题 jQuery Bug Ticket
I believe this is your issue jQuery Bug Ticket
使用自定义属性代替
for
;例如,target_ele
。你的编辑会对你大喊大叫,但它会起作用。
Instead of
for
use a custom attribute instead;target_ele
for example.Your editor will yell at you, but it will work.