1

I have a situation where I need to search multiple "li" s each for a certain different string, and if it meets the conditions in all of them, append a link to only the last one.

I am using jQuery :contains() to find out if each li has a certain word in it, and i can make it .append() to all of the li's based on the multiple conditions, but I only need it to append to the last li.

How do I get that to work? I can get it to append to all using:

$('li#1:contains("x"),li#2:contains("y")').append('z');

this gives me:

xz
yz

I need it to only give me:

x
yz
4

1 回答 1

0

您可以使用“下一步”:

$(document).ready(function(){
    var target = $('li#1:contains("x")').next('li#2:contains("y")');
    if (target){
        target.append('z');
    }
    //$('li#1:contains("x")').next('li#2:contains("y")').last().append('z');
});

小提琴:http: //jsfiddle.net/vuWuD/3/

编辑:更新地址评论。

于 2013-08-27T02:11:32.570 回答