1

私はcontainsjqueryで問題を抱えています。一言しか受け付けないようです。フレーズや単語ではありません。

例:

$('#div:contains('Word')'); --> This is okay

$('#div:contains('Just another word')'); --> This will return empty/this will not match.

この種の問題の経験はありますか?

ご返信いただきありがとうございます。

4

3 に答える 3

4

必要なのは、二重引用符を使用することです (代わりに、セレクター全体を囲む単一引用符)。たとえば、次のようになります。

$("p:contains('John Resig')");

これにより、文字列「John Resig」が内部にある正しい段落が選択されます

またはそれを逆にすることができます:

$('p:contains("John Resig")');

または、古き良きエスケープを使用できます。

$('p:contains(\'John Resig\')');
于 2009-10-16T23:28:58.957 に答える
1

# なしで試してみて、コード内で同じ引用符を使用しないでください:-

$("div:contains('Just another word')");

于 2009-10-16T23:32:27.373 に答える
0

Assuming you didn't already solve this, from your comment which included:

var selected = $(this).find(':selected').text(); 
if (selected.toLowerCase() != "all industries") { 
    if (trim(selected).length > 0) { 
        $("#contact-list-selectable li .contact-info-industry").parent().css("display", "none"); 
        $("#contact-list-selectable li .contact-info-industry:contains('" + selected + "')").parent().css("display", "block"); 
        alert($("li[style='display: block;']").text()); 
}

I'll assume you meant to use trim as a method on the string, or if not that you have a trim function defined somewhere else. If this is actually how your code looks and you have no custom trim function, then that's going to be your first problem, it should be: selected.trim().length

One potential issue is that while you check that the trim of selected is > 0, you don't use a trimmed version of selected when checking the contains. If you have any spaces/etc in your selected variable, it will fail for contains.

Here is an example. Note the trailing space in selected = "computer science "; that is intentional to demonstrate what happens in that case.

if you change

 $("#contact-list-selectable li .contact-info-industry:contains('" + selected + "')").parent().css("display", "block");

to

 $("#contact-list-selectable li .contact-info-industry:contains('" + selected.trim() + "')").parent().css("display", "block");

you can avoid this issue (working example, see here, note the trailing space is still there).

The only other issue I could think of would be if you were incorrectly forming any of your selectors and they did not actually match your DOM structure. Otherwise everything works fine in current jquery.

于 2013-01-11T00:10:03.063 に答える