代码之家  ›  专栏  ›  技术社区  ›  Rohit Agarwal

jQuery选择器帮助

  •  1
  • Rohit Agarwal  · 技术社区  · 16 年前

    我有一个很大的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>    
    

    例如,要选择DIV“q”中的SPAN,我尝试使用以下方法:

    $("#q").find("#title"); 
    

    这在FF和Chrome上运行得很快,但在IE8(和IE7)中执行find()方法需要很长时间。我还有别的办法吗?

    请让我知道如果我可以提供任何进一步的信息。

    3 回复  |  直到 16 年前
        1
  •  8
  •   user113716    16 年前

    您应该更改标记以使用 <span>

    <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> 
    

    $("#q").find(".title");
    

    如果我没记错的话(尤其是在非唯一ID上)。把你的代码改成上面的可能对你有用。

        2
  •  2
  •   Doug Neiner    16 年前

    就像其他人说的,代码是无效的。但是,如果您被它卡住了,您应该能够这样做来正确地选择它。如果不运行测试,我不确定这与您尝试的其他方法相比性能如何:

    $("#b span[id=title]");
    
        3
  •  0
  •   Marko    16 年前

    像这样的怎么样

    $("#q").find("span").each(function() {
        if($(this).attr("id") == "title") {
            $(this).css('color','red');
        }
    });