Jquery隐藏/显示元素

Jquery hide/show elements

本文关键字:元素 显示 隐藏 Jquery      更新时间:2023-09-26

我正在尝试用javascript实现这一点,但经过了更优化,切换功能也能正常工作!

我的js代码:

$(document).ready(function() {
    $('a.details').click(function(e){
        var id= '';
        $('a.details').each(function() {
            id = $(this).attr('href');
            $('#'+id).hide();
        });
        $(this).addClass('active');
        id = $(this).attr('href');
        $('#'+id).toggle();
        e.preventDefault();
   });
});

以下是我的看法,除了在行的ID中添加一个t之外,没有更改html

http://jsfiddle.net/mplungjan/Rfn8z/

欢迎评论(特别是如果投票否决)

$(document).ready(function() {
  $('a.details').each(function() {
    var tr = $("#t"+parseInt($(this).html()));
    var link = this;
    $(this).toggle(
      function(e){tr.show(); $(this).addClass('active');   e.preventDefault();},
      function(e){tr.hide(); $(this).removeClass('active');e.preventDefault();}
    );
  });
});

糟糕的做事方式。相反,看看这个:

http://jsfiddle.net/xzpkq/

也许你会受到启发,产生更好的代码