代码之家  ›  专栏  ›  技术社区  ›  orandov

我可以在jquery属性等于选择器([attribute=value])中放一个单引号吗?

  •  11
  • orandov  · 技术社区  · 15 年前

    我的HTML如下所示:

            <a href="#" id="QuoteTest">Click Here</a>
            <ul>
                <li title="this" style="position:relative">one</li>
                <li title="this" style="position:relative">two</li>
                <li title="tha't" style="position:relative" >three</li>
                <li title="tha't" style="position:relative">four</li>
             </ul>
    

    jQuery:

    $('a#QuoteTest').click(function() {
        $('li[title=this]').animate({ 'left': '+=40px' }, 'slow');
        $("li[title=tha't]").animate({ 'top': '+=40px' }, 'slow');
    });
    

    我不能让选择器使用其中的单引号。我试图用反斜杠“\”来转义引号,但这没有帮助。

    有什么建议吗?

    3 回复  |  直到 15 年前
        1
  •  13
  •   redsquare    15 年前

    试试两个

    $("li[title=tha\\'t]").animate({ 'top': '+=40px' }, 'slow');

        2
  •  5
  •   Captain Obvious    15 年前

    在IE中这些都不适用于我,所以我必须使用filter():

    $("li").filter(function(index){
        return $(this).attr("title") == "tha\'t"; 
    })
    .animate({ 'top':'+=40px' }, 'slow');
    
        3
  •  3
  •   Gabriel Hurley    15 年前

    我觉得你需要一个双反斜杠,很奇怪。关于jquery如何避开这些字符串的一些内容。所以你会有:

    $("li[title=tha\\'t]").animate({ 'top': '+=40px' }, 'slow');
    

    在其他情况下就是这样。让我知道这次是否有效。