开发者

How to get ul class name while clicking on li element

开发者 https://www.devze.com 2023-04-11 05:08 出处:网络
How to get ul class name while clicking li - jquery <div class=\"div-class-parent\"> <div class=\"div-class\">test</div>

How to get ul class name while clicking li - jquery

<div class="div-class-parent">
    <div class="div-class">test</div>
    <ul class="tabs">
        <li class="node-205"></li>
        <li class="node-150"></li>
        <li class="node-160"></li>
    </ul>
</div>

EDITED:

For example when i clicking the "li" I need to get the class name of ul and class name of the div.

That is i need the class name "tabs" and "div-class".

When i use the below code i get the parent class name of div that is i get "div-class-parent".

$('li').click(function() {
   alert($(this).closest(开发者_开发百科'div').attr('class'));
});

How can this be done using jquery.

thanks in advance...


Simply reference to the parent:

$('li').click(function() {
   alert($(this).parent().attr('class'));
});

or find closest element:

$('li').click(function() {
   alert($(this).closest('ul').attr('class'));
});

EDIT

$('li').click(function() {
   var $ul = $(this).closest('ul');
   alert($ul.attr('class')); // ul class
   alert($ul.prev().attr('class')); // div class
});


$('li').click(function(){
    alert($(this).parent().attr('class'))
})


Try this simple fiddle.

HTML

<div class="div-class-parent">
    <div class="div-class">test</div>
    <ul class="tabs">
        <li class="node-205"></li>
        <li class="node-150"></li>
        <li class="node-160"></li>
    </ul>
</div>

JS

$("li").click(function(){
    alert($(this).parent().attr("class"));
    alert($(this).parent().prev().attr("class"));
});
0

精彩评论

暂无评论...
验证码 换一张
取 消