English 中文(简体)
履历
原标题:Jquery buttons background color alternate
  • 时间:2011-10-05 16:27:21
  •  标签:
  • jquery

www.un.org/spanish/ga/president 如果点击<><<>t>for,则该行的against纽吨有备用颜色。 我试图用 j子做这件事,但却是 t。 如何确定这一点? http://jsfiddle.net/mdanz/9H9qE/1/“rel=“nofollow” http://jsfiddle.net/mdanz/9H9qE/1/。

    <style type="text/css">
.rate3 {
padding:5px;
font-family:Arial;
color:#FFFFFF;
cursor:pointer;
background-color:red;
border:1px solid #000000;
}

.rate4 {
padding:5px;
font-family:Arial;
color:#FFFFFF;
cursor:pointer;
background-color:red;
border:1px solid #000000;
}

.vote {
height:30px;
width:auto;
margin-bottom:10px;

}
</style>
<script type="text/javascript">
$( .rate3 ).live( click ,function() {

    $(this).css( background-color , #FFFFFF );
        $(this).css( color , red );
        $(this).parent( div ).next( .rate4 ).css( background-color , red );
        $(this).parent( div ).next( .rate4 ).css( color , #FFFFFF );

}

$( .rate4 ).live( click ,function() {

    $(this).css( background-color , #FFFFFF );
        $(this).css( color , red );
        $(this).parent( div ).next( .rate3 ).css( background-color , red );
        $(this).parent( div ).next( .rate3 ).css( color , #FFFFFF );

}
</script>
<?php for($i=0;$i<11;$i++) {  ?>
<div class="vote"><a class="rate3">For</a> <a class="rate4">Against</a></div>

<?php  } ?>
最佳回答

您不需要<代码>.live(点击),因为你 t动地用j Query对OM进行更改,因此只使用<代码>.click(。

该守则应当发挥作用:

$( .vote a ).click(function() {
    $(this).siblings().css({
        backgroundColor:  red ,
        color:  white 
    });
    $(this).css({
        backgroundColor:  white ,
        color:  red 
    });
});

Demo:


但我更喜欢用班级这样做,因为用风格表对我来说更清洁(更容易):

$( .vote a ).click(function() {
    $(this).addClass( selected ).siblings().removeClass( selected );
});

以及适当的社会保障局:

.selected {
    background-color: white;
    color: red;
}

页: 1 http://jsfiddle.net/9H9qE/22/

问题回答

如下:

$( a ).click(
    function(){
        if ($(this).is( .rate3 )){
            c =  .rate4 ;
        }
        else {
            c =  .rate3 ;
        }

        $(this).closest( .vote ).find(c).css({
            backgroundColor :  #fff 
        });
    });

JS Fiddle demo

在Blender的答复中,你还可以清理你如何重新确定像这样的特别安全局的财产:

$(this).css({
    backgroundColor:  white ,
    color:  red 
});

http://jsfiddle.net/9H9qE/16/“rel=“nofollow”>example





相关问题
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: &...

热门标签