0

###HTML

<ul id="thiselement">
    <li>test</li>
    <li>timer</li>
    <li>check</li>
    <li>ramramesh</li>
    <li>ramesh</li>
</ul>

###jQuery

document.write($("#thiselement li:not(:contains('ramesh'))").text());

###预期结果

testtimercheckramramesh

###我得到了什么

testtimercheck

我怎样才能做到这一点?我知道它contains会在找到文本的地方返回,但我只需要确切的文本选择器,因为我想将每个函数与结果一起使用

$("#thiselement li:not(:contains('ramesh'))").each(function(){});

JSfiddle

4

3 回答 3

5
var names = $('ul li').map(function() {
    if ( $(this).text() !== 'ramesh' ) return $(this).text();
}).get().join();

https://jsfiddle.net/j3LKd/

于 2013-06-07T10:41:28.150 回答
3

请查看https://jsfiddle.net/dntAU/2/

$('ul li').each(function(){
    var text = $(this).text()
    if ( text == 'ramesh'){
      alert(text + " is ramesh ")   
    }else{
    alert(text + " is not ramesh ");
    }
});

$('#thiselement li:contains("ramesh")').filter(
    function(){
        if( $(this).text() == 'ramesh'){
            $(this).addClass('highlight');
        }
    });
于 2013-06-07T10:47:09.237 回答
0

扩展到 vinothini 答案是遵循JSFiddle,但我仍然期待其他一些可能性

$('ul li').each(function(){
    var text = $(this).text()
    if ( text == 'ramesh'){
      alert(text + " is ramesh ")   
    }else{
    alert(text + " is not ramesh ");
    }
});

$('#thiselement li').filter(
    function(){
        if( $(this).text() != 'ramesh'){
            $(this).addClass('highlight');
        }
    });


$('#thiselement li').each(
    function(){
        if( $(this).text() != 'ramesh'){
            $(this).addClass('back');
        }
    });

CSS

.highlight {
    color: white;
}

.back{background:blue;}
于 2013-06-07T12:35:50.183 回答