문제

//first ul    
<ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    //second ul
        <ul >
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>

I want to write some text only into all the <li> elements of 2nd unordered list. I want my jquery to ignore <li>s in first <ul>

Here is what I attempted, but it did not work:

$("ul:nth-child(1) li").eq(1).each(function(){
                $(this).text("Lorem Ipsum");

            });
도움이 되었습니까?

해결책 2

I want to write some text only into all the <li> elements of 2nd unordered list.

You can use :eq(index)

Select the element at index n within the matched set.

Note: Zero-based index of the element to match

Use

$("ul:eq(1) li").text("Lorem Ipsum")

DEMO

EDIT: Be wise to chooses between :eq() and :nth-child()

See comparative example

Demo with :eq() vs Demo with :nth-child()

다른 팁

Try this

$('ul:nth-child(2) li').each(function(){
    $(this).text("Lorem");
});

DEMO HERE

Try

$("ul:eq(1)").find("li").each(function(){
  $(this).text("Lorem Ipsum");
});

:eq(1) selects the element with index 1. Then you can use .find() to get the child elements.

Try to use the :eq() selector,

$("ul:eq(1) > li").each(function(){
   $(this).text("Lorem Ipsum");
});

DEMO

if you want to find out all the 'ul" of page then use below code

$j("ul").each(function () {

});

if you want to find out specific "ul" under specific class or element then use below code

$j(".ClassName ul").each(function () {

});


$j("#ParentElementId ul").each(function () {

});

You need to select nth-child(2) not 1 and not eq(1) since you need all list:

$("ul:nth-child(2) li").each(function(){
     $(this).text("Lorem Ipsum");
});

As difference notified by @Rajaprabhu you may wish to use eq() method instead of nth-child():

$("ul:eq(1) li").each(function(){
    $(this).text("Lorem Ipsum");
});

you could give a Id to your second ul and selected directly

$('#anyId li').each(function(){
    $(this).text("Lorem Ipsum");
});

You can use

$("ul:eq(X) li").text("Text to write in second ul")

Where X = what UL you want. Here is an example: http://jsfiddle.net/Q2gj6/2/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top