JQuery选择器中的嵌套ID [英] Nested ID in JQuery selector
问题描述
我正在尝试更改嵌套在称为blink3的div中的嵌套的href:
I am trying to change the href of an a nested in a div called blink3:
$('#blink3 #backLink').attr('href',"#item5");
其中的html代码如下:
<div style="float:left;text-align:center;" id="blink3">
<a href="#item4" class="panel" id="backLink">
<div class="divBack"></div></a>
</div>
在所有浏览器中都能正常运行-不是IE 7 ,我不知道为什么,不更改href.
works well in all browsers - NOT IE 7 and I cannot figure out why, doesn't change the href.
这是IE 7的错误吗? jQuery的?我的代码有什么问题吗?
Is this a IE 7 bug? JQuery? anything wrong with my code?
有解决方案吗?
推荐答案
尼克·克拉弗(omg,您去过哪里?)已经在评论中指出了这一点.创建像#id #id
这样的选择器完全没有意义,因为根据定义,ID必须在您的DOM中是唯一的.
Nick Craver (omg, where have you been around?) already stated it in a comment. It totally makes no sense to create a selector like #id #id
because an ID has to be unique within your DOM by definition.
您似乎违反了该规则,因此jQuery可能选择了错误的节点.
It looks like you break that rule and therefore, jQuery might select a wrong node.
您应将id
的反向链接"替换为classes
,并选择类似的选择器
You should replace your "backlink" id
's with classes
and go for the selector like
$('#blink3 .backLink');
这篇关于JQuery选择器中的嵌套ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!