【问题标题】:Create a toggle function onclick创建一个切换功能 onclick
【发布时间】:2013-11-14 17:54:04
【问题描述】:

我正在尝试在点击时执行切换功能。 不明白为什么我的函数不起作用,很简单……

http://jsfiddle.net/7y46W/1/

 $("#hello").toggle(function(){
 $('#content').css({marginLeft:'20px'});
     },function(){
 $('#content').css({marginLeft:'0px'});
 });

【问题讨论】:

标签: javascript jquery function toggle


【解决方案1】:
$("#hello").on('click', function(){
    var toggle = $(this).data('toggle');  // get current state

    $('#content').css('margin-left', toggle ? 0 : 20);

    $(this).data('toggle', !toggle);      // set state to opposite of current
});                                       // state, i.e. a toggle switch

FIDDLE

【讨论】:

  • 稍微解释一下会有所帮助!!
  • 应该不言自明?
  • 是的,为了满足您的解释要求?
【解决方案2】:

它确实有效...当您包含 jQuery 时。 见fiddle

included jQuery in framework selector.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2013-10-16
    相关资源
    最近更新 更多