我有一个大的HTML文档,大致有这种结构:
<div id="a">
<!-- more html code here -->
<span id="title">...</span>
<!-- more html code here -->
</div>
<div id="b">
<!-- more html code here -->
<span id="title">...</span>
<!-- more html code here -->
</div>
...
...
<div id="z">
<!-- more html code here -->
<span id="title">...</span>
<!-- more html code here -->
</div>
Run Code Online (Sandbox Code Playgroud)
请注意,外部DIV具有唯一的ID("a","b",...,"z"),但内部SPAN具有非唯一的ID("标题").
例如,要选择DIV"q"内的SPAN,我尝试使用它:
$("#q").find("#title");
Run Code Online (Sandbox Code Playgroud)
这在FF和Chrome上运行得很快,但find()方法需要很长时间才能在IE8(和IE7)中执行.还有其他方法可以做到这一点吗?
如果我能提供任何进一步的信息,请告诉我.
您应该更改标记以使用<span>元素的类而不是ID,因为ID必须是唯一的.
<div id="a">
<!-- more html code here -->
<span class="title">...</span>
<!-- more html code here -->
</div>
<div id="b">
<!-- more html code here -->
<span class="title">...</span>
<!-- more html code here -->
</div>
...
...
<div id="z">
<!-- more html code here -->
<span class="title">...</span>
<!-- more html code here -->
</div>
Run Code Online (Sandbox Code Playgroud)
然后:
$("#q").find(".title");
Run Code Online (Sandbox Code Playgroud)
如果我没记错的话,IE尤其会遇到非唯一ID问题.将代码更改为上面可能会为您解决问题.