简体   繁体   English

将两个不同的jquery click事件分配给同一项目

[英]Assign two different jquery click events to same item

I have a button on my site that has jquery that changes some css of other elements on click. 我的网站上有一个带有jquery的按钮,可在单击时更改其他元素的某些CSS。

I want to assign another function to reverse the css changes when the button is clicked a second time. 当第二次单击按钮时,我想分配另一个功能来反转css更改。

$('.mobileitem').click(function(){
   $('.bottomfooter').css("bottom","75px");
   $('#mobilefoot').css("display", "block");
}); 

I want to be able to click .mobileitem again and have the css change to bottom:0 display:none for their respective elements. 我希望能够再次单击.mobileitem并将其相应元素的css更改为bottom:0 display:none。

Each time I click the .mobileitem it should go between the two. 每次我单击.mobileitem时,它都应该位于两者之间。

I think it is .toggle() but not sure of the proper syntax or if there is a better way 我认为这是.toggle(),但不确定正确的语法或是否有更好的方法

$('.mobileitem').click(function(){
   var bot_val="75px";
   var dis_val="block";
   if($('.bottomfooter').css("bottom")==bot_val){
       bot_val="0px";
   }
   if($('#mobilefoot').css("display")==dis_val){
       dis_val="none";
   }
   $('.bottomfooter').css("bottom", bot_val);
   $('#mobilefoot').css("display", dis_val);
});

This should work! 这应该工作!

Try this 试试这个

function toggleClickEvents(item, click1, click2){
    $(item).off('click').on('click', function(){
        click1();
        toggleClickEvents(item, click2, click1);
    });
}

Or just use .toggle() although it is deprecated and possibly removed. 或仅使用.toggle()(尽管已弃用并可能将其删除)。 (Not sure what the replacement is) (不确定替换是什么)

$('.mobileitem').toggle(function(){
   $('.bottomfooter').css("bottom","75px");
   $('#mobilefoot').css("display", "block");
}, function(){
   $('.bottomfooter').css("bottom","0px");
   $('#mobilefoot').css("display", "none");
});

Here's a neater, cleaner example using TOGGLE functionality. 这是使用TOGGLE功能的更整洁,更简洁的示例。 It'll work as well. 也可以。 :) :)

you can write two css classes 您可以编写两个CSS类

.bottom75
{
  bottom: 75px;
  display: block;
}

.bottom0
{
  bottom: 0px;
  display: none;
}

On click event 点击事件

$('.mobileitem').click(function(){
  $(this).hasClass('bottom75') ? $(this).addClass('bottom0'): $(this).addClass('bottom75');
});

try this: 尝试这个:

$('.mobileitem').click(function(){
   $(".bottomfooter, #mobilefoot").toggle(function() {
   $('.bottomfooter).css('bottom','75px');
   $('#mobilefoot').css('display', 'block');
   }, function () {
   $('.bottomfooter').css('bottom','0');
   $('#mobilefoot').css('display', 'none');
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM