jQuery Find and List all LI elements within a UL within a specific DIV

后端 未结 4 1460
终归单人心
终归单人心 2020-12-09 02:49

I have 3 Columns of ULs each a Dynamic UL container that can have anywhere from 0-9 LI containers (eventually more). All my LI elements have an attribute \"rel\" which I am

相关标签:
4条回答
  • 2020-12-09 02:58
    var column1RelArray = [];
    $('#column1 li').each(function(){
        column1RelArray.push($(this).attr('rel'));
    });
    

    or fp style

    var column1RelArray = $('#column1 li').map(function(){ 
        return $(this).attr('rel'); 
    });
    
    0 讨论(0)
  • 2020-12-09 03:11

    html

    <ul class="answerList" id="oneAnswer">
        <li class="answer" value="false">info1</li>
        <li class="answer" value="false">info2</li>
        <li class="answer" value="false">info3</li>
    </ul>   
    

    Get index,text,value js

        $('#oneAnswer li').each(function (i) {
    
            var index = $(this).index();
            var text = $(this).text();
            var value = $(this).attr('value');
            alert('Index is: ' + index + ' and text is ' + text + ' and Value ' + value);
        });
    
    0 讨论(0)
  • 2020-12-09 03:14

    Are you thinking about something like this?

    $('ul li').each(function(i)
    {
       $(this).attr('rel'); // This is your rel value
    });
    
    0 讨论(0)
  • 2020-12-09 03:15
    $('li[rel=7]').siblings().andSelf();
    
    // or:
    
    $('li[rel=7]').parent().children();
    

    Now that you added that comment explaining that you want to "form an array of rels per column", you should do this:

    var rels = [];
    
    $('ul').each(function() {
        var localRels = [];
    
        $(this).find('li').each(function(){
            localRels.push( $(this).attr('rel') );
        });
    
        rels.push(localRels);
    });
    
    0 讨论(0)
提交回复
热议问题