English 中文(简体)
JQuery: 如何仅对目前DIV的要素适用点击功能?
原标题:JQuery: How to only apply click function to elements in the current DIV?
  • 时间:2011-11-23 03:18:25
  •  标签:
  • jquery

我有多个称为“<代码>.details的散射机,我想对他们适用以下点击功能。 但是,现在,如果我点上了一条电线,那么所有电离层名称都在整个称为<代码>的圆形/代码上发生变化。 我如何将点击功能运用到目前的<代码>.details div?

$(document).ready(function() {
    $( .details div.two ).hide();
    $( .details div.three ).hide(); 

    $( .details ul li.one ).click(function () { 
        $(this).addClass( active );
        $( .details ul li.two ).removeClass( active );
        $( .details ul li.three ).removeClass( active );
        $( .details div.one ).show();
        $( .details div.two ).hide();
        $( .details div.three ).hide(); 
    });
    $( .details ul li.two ).click(function () { 
        $(this).addClass( active );
        $( .details ul li.one ).removeClass( active );
        $( .details ul li.three ).removeClass( active );
        $( .details div.one ).hide();
        $( .details div.two ).show();
        $( .details div.three ).hide(); 
    });
    $( .details ul li.three ).click(function () {   
        $(this).addClass( active );
        $( .details ul li.one ).removeClass( active );
        $( .details ul li.two ).removeClass( active );
        $( .details div.one ).hide();
        $( .details div.two ).hide();
        $( .details div.three ).show(); 
    });
});

传真

<div class="details">
    <ul>
        <li class="one active"><span>Nav 1</span></li>
        <li class="two"><span>Nav 2</span></li>
        <li class="three"><span>Nav 3</span></li>
    </ul>
    <div class="one">
        <p>Content 1</p>
    </div>
    <div class="two">
        <p>Content 2</p>
    </div>
    <div class="three">
        <p>Content 3</p>
    </div>                                                                          
</div>                          

CSS

.active {background:#fff;}
最佳回答
问题回答

您可使用<条码> 封顶,以找到与<条码>详细内容/代码”相近的祖先,并用于找到适当的要素。

例如:

$( .details ul li.two ).removeClass( active );

将:

$(this).closest( .details ).find( ul li.two ).removeClass( active );

我这样说:

<div class="details">
    <ul>
        <li class="one active" data-divclass="one"><span>Nav 1</span></li>
        <li class="two" data-divclass="two"><span>Nav 2</span></li>
        <li class="three" data-divclass="three"><span>Nav 3</span></li>
    </ul>
    <div class="one">
        <p>Content 1</p>
    </div>
    <div class="two">
        <p>Content 2</p>
    </div>
    <div class="three">
        <p>Content 3</p>
    </div>                                                                          
</div>  

<script type= javascript >

    $(document).ready(function() {
        $( .details ul li ).click(function () { 
            var $this=$(this);
            $( .details ul li ).removeClass( active );
            $this.addClass( active );
            $( div.details div ).hide();
            $( div.details div. +$this.data( divclass )).show();
        });
    });

</script>

。 CSS

.active {background-color: #999;}
div.two, div.three {display: none;}

demo page -





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签