【问题标题】:jquery toggling a tag but it should behave click and again click onlyjquery切换标签,但它应该表现为点击并再次点击
【发布时间】:2013-08-06 09:46:09
【问题描述】:

html

<ul>
    <li><a href="#">one</a></li>
    <li><a href="#">two</a></li>
</ul>
<br clear="all" />
<div>some text</div>

css

ul{
    list-style: none;
}
li{
    float: left;
    margin: 0 20px;
}
div{
    background-color: red;
    height: 200px;
}

jquery

$('li a').toggle(function(e){
   $('div').toggle(); 
    e.preventDefault();
});

demo

我的关键问题是为什么一个标签获得了切换功能,而它应该只切换 div。

编辑

$('li a').toggle() 函数是点击再点击函数。但是 $('div').toggle() 功能是显示和隐藏。我对吗?

【问题讨论】:

    标签: jquery


    【解决方案1】:

    toggle 更改为click

    a 标记正在获取 toggle,因为您正在使用 .toggle() 事件。

    $('li a').click(function(e){ //change toggle to click
       $('div').toggle(); 
        e.preventDefault();
    });
    

    $('li a').toggle()函数需要这样使用

    $('li a').toggle(function(){},function(){})

    DEMO

    $('li a').toggle(function (e) {
        $('div').toggle();
        alert('1');
        e.preventDefault();
    }, function (e) {
        $('div').toggle();
        alert('2');
        e.preventDefault();
    });
    

    【讨论】:

      【解决方案2】:

      你会这样吗:demo

      $('li a').toggle(function(e){
         $('div').hide(); 
       e.preventDefault();
      }, function(){ 
          $('div').show();
      e.preventDefault();
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多