开发者

jQuery add border to table

开发者 https://www.devze.com 2022-12-28 05:41 出处:网络
I\'m new to jQuery, I tried this: <input value=\"1\"type=\"checkbox\" name=\"mytable\" id=\"checkbox2\"style=\"float:left;\"

I'm new to jQuery, I tried this:

<input value="1"  type="checkbox" name="mytable" id="checkbox2"  style="float:left;"
    开发者_开发知识库    />

{literal}

<script src="http://code.jquery.com/jquery-latest.js"></script>
            <script type="text/javascript">
$(function() {
   //checkbox
   $(".mytable").click(function(){
    $(".mytable").toggleClass('mytableborders');

    });
});
</script>
{/literal}

<table class="mytable" id="cart">....</table>

But it doesn't work, I want the checkbox to change the class of the table from .mytable to .mytableborders.


Your checkbox id is "checkbox2". So the selector for your checkbox is $("#checkbox2"), and your table id is "cart" and selector is $("#cart")

Try

$("#checkbox2").click(function(){
    $("#cart").toggleClass('mytableborders');
});


$(function() {
  $("#checkbox2").click(function(){
    $("#cart").toggleClass('mytableborders');
  });
});

The click event should be attached to the checkbox by ID, not name.


Toggle class will add the class if it's not present and remove it if it is. If you want it to swap the classes, use addClass and removeClass:

    $(".mytable").removeClass('mytable').addClass('mytableborders');
0

精彩评论

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