Get attribute of child element

前端 未结 5 451
温柔的废话
温柔的废话 2020-12-30 23:28

If I have the following markup:

Hours&am

相关标签:
5条回答
  • 2020-12-31 00:03
    $("a span[sortcat]").attr('sortcat')
    

    That'll give you the first element's sortcat value. To get all of them, do this:

    $("a span[sortcat]").map(function(){ return $(this).attr('sortcat') })
    

    See this working demo: http://jsfiddle.net/BwgDW/

    0 讨论(0)
  • 2020-12-31 00:04

    There's a couple of ways you can reference the span tag, but all of them end with " .attr('sortcat'); " I guess it depends on how specific you want to be and how flexible you need to be if there's a few other p tags with anchor tags and spans inside.

    $('p.header a.sort span.imgholder').attr('sortcat');
    
    /* or */
    
    $('span.imgholder').attr('sortcat');
    

    You can select elements based upon their tag name, their class name, or by the attributes inside the tags. Refer to jQuery's documentation on selectors:

    http://api.jquery.com/category/selectors/basic-css-selectors/

    http://api.jquery.com/category/selectors/

    0 讨论(0)
  • 2020-12-31 00:05
    $(".sort").click(function(){
      var cat =  $(this).children("span").attr("sortcat");
      //do something with the sortcat
    });
    
    0 讨论(0)
  • 2020-12-31 00:13
    $('.sort span')
    

    Did I misunderstand?

    0 讨论(0)
  • 2020-12-31 00:27

    find() finds elements within a given element.

    $('a.sort').find('span');

    0 讨论(0)
提交回复
热议问题