简体   繁体   English

我的jQuery click事件仅工作一次

[英]My jQuery click event only works once

I have a set of div's(75px by 75px). 我有一组div(75px x 75px)。 Using JQ I"m trying to make it so when you click on the div, it becomes 500px by 500px, and if you click it again it returns to it original size. I can make it big, but it doesn't respond to any further clicks. my latest attempt: 我正在尝试使用JQ,因此,当您单击div时,它会变成500px x 500px,如果再次单击它,它将返回到其原始大小。我可以将其放大,但是它不会响应任何大小我的最新尝试:

$(document).ready(function () {

  $(".option").on("click", function () {

    if ($(this).width > ("100px")) {

      $(this).css("width", "500px");
      $(this).css("height", "500px");
      $(this).css("background-color", "#FFB2B2");
      $(this).css("background-color", "#FFB2B2");
    }
  });

  $(".option").on("click", function () {

    if ($(this).width < ("100px")) {
      promt("dumb")
      $(this).removeAttr("style")
      $(this).css("height", "");
      $(this).css("background-color", "");
      $(this).css("background-color", "");
    }
  });
});

I've also tried 我也尝试过

$(document).ready(function () {

  $(".option").on("click", function () {

    $(this).css("width", "500px");
    $(this).css("height", "500px");
    $(this).css("background-color", "#FFB2B2");
    $(this).css("background-color", "#FFB2B2");
  });

  $(".option").on("click", function () {
    $(this).off("click");
  });
});

I would like to note: I've created a dblclick event. 我想指出:我创建了一个dblclick事件。 like one click makes it big, dblclick makes it small. 就像一键放大一样,dblclick缩小它。 Its works but not very user friendly. 它的作品,但不是非常用户友好。

Your problems are in your if statements, $.width is a function and you are comparing it to a string. 您的问题出在您的if语句中,$。width是一个函数,您正在将其与字符串进行比较。

  • first you have to correct these and call the function by changing $(this).width to $(this).width(). 首先,您必须更正这些错误并通过将$(this).width更改为$(this).width()来调用函数。 now this statement returns a number. 现在,此语句返回一个数字。
  • the second problem is you are comparing a number with the string "100px". 第二个问题是您正在将数字与字符串“ 100px”进行比较。 to solve this change "100px" to just 100. 解决此更改,将“ 100px”更改为100。
  • your third problem is your if logic is reversed, you are first checking if the width is greater than 100 and then setting it to 500x500 which is STILL greater than 100 reversing this symbol would fix that 您的第三个问题是,如果逻辑取反,则首先要检查宽度是否大于100,然后将其设置为500x500(仍大于100),反转此符号可以解决该问题
  • your fourth problem is you attach two event handlers that will run one right after the other. 您的第四个问题是,您附加了两个事件处理程序,它们将在另一个之后立即运行。 so it will always return to original state. 因此它将始终返回到原始状态。 move the second statement into the first event and set it as an else. 将第二条语句移到第一个事件中并将其设置为其他事件。

Bonus You can also pass an object instead of doing multiple calls to css when you want to change more than one css attribute at a time. 奖励当您想一次更改多个CSS属性时,也可以传递一个对象,而不是多次调用CSS。 like so: 像这样:

$(this).css({ width: 500,
              height: 500,
              'background-color': '#FFB2B2' });

This is an answer to address the problems with your code. 这是解决您的代码问题的答案。 But to actually implement this the way that is widely acceptable please refer to the answers that toggle a class name 但是要以一种可以广泛接受的方式实际实施此方法,请参考切换类名的答案

 $(".option").on("click", function () { if ($(this).width() < 100) { $(this).css("width", "500px"); $(this).css("height", "500px"); $(this).css("background-color", "#FFB2B2"); } else { $(this).css("width", ""); $(this).css("height", ""); $(this).css("background-color", ""); } }); 
 .option{ background-color: #888; width: 75px; height: 75px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="option"></div> 

As mentioned in the comments $(this).width() should be used. 如注释中所述,应使用$(this).width()。 It always returns an integer in px. 它总是返回一个以px为单位的整数。 Don't try and use strings to compare numbers. 不要尝试使用字符串来比较数字。 Using string comparisons will give funny results since it does a character by character comparison. 使用字符串比较会产生有趣的结果,因为它会逐个字符地进行比较。

for example: the string "1000" is less than the string "5" because the character "1" comes before "5" alphabetically. 例如:字符串“ 1000”小于字符串“ 5”,因为字符“ 1”按字母顺序位于“ 5”之前。

Don't create two simultaneous conflicting click() s bound on the same element. 不要在同一元素上创建两个同时冲突的click() Also $(this).width should be $(this).width() etc... 另外$(this).width应该是$(this).width()等...

Instead, use only one .click() handler, and use .toggleClass() to toggle your large class on the $(this) clicked element: 相反,仅使用一个.click()处理函数,并使用.toggleClass()$(this) clicked元素上切换大型类:

 $(".option").click(function(){ $(this).toggleClass("large"); }); 
 .option{ width:100px; height:100px; background: #FFB2B2; transition: 0.3s; -webkit-transition: 0.3s; } .large{ /* Added by jQuery on click */ background: red; width:500px; height:500px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="option"></div> 

PS: now after the above solution it's unrelated, but for your future notice use .css({}) like: 附言:现在上述解决方案之后,它是无关紧要的,但是为了以后通知,请使用.css({})例如:

$(this).css({
  width : "500px",
  height : 500,                // or Number if px are the expected unit
  backgroundColor : "#ffb2b2"
});

I would do it like this: 我会这样做:

 // external.js $(function(){ $('.option').click(function(){ $(this).toggleClass('big'); }); }); 
 /* external.css */ .option{ width:200px; height:200px; background:yellow; } .big{ width:500px; height:500px; background-color:#FFB2B2; } 
 <!DOCTYPE html> <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'> <head> <meta http-equiv='content-type' content='text/html;charset=utf-8' /> <link type='text/css' rel='stylesheet' href='external.css' /> <script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script> <script src='external.js'></script> </head> <body> <div class='option'>Why do I bother?</div> </body> </html> 

toggleClass is a much cleaner approach if you are trying to switch between two states. 如果您尝试在两种状态之间切换,那么toggleClass是一种更为简洁的方法。

this will simply apply css for an active state and remove it when deselected. 这将简单地将CSS应用于活动状态,并在取消选中状态时将其删除。 this way you dont have to worry about binding on and off a click event. 这样,您不必担心绑定Click事件的问题。 i made a simple jsfiddle for example: 我做了一个简单的jsfiddle例如:

$(document).ready(function() {
    $( "div" ).click(function() {
      $( this ).toggleClass( "grow" );
  });
})

https://jsfiddle.net/Lrzysvgr/ https://jsfiddle.net/Lrzysvgr/

Thanks for the replies!!! 感谢您的答复!!! I finally put my pride aside and asked a much more experience guy in the same room as me. 我终于放下了骄傲,在与我同一个房间里问了一个经验丰富的家伙。 This is what he did and it works perfect. 这就是他所做的,而且效果很好。

$(document).ready(function () { $(document).ready(function(){

 $(".option").on("click", function () { if ($(this).css("width") != "500px") { $(this).css("width", "500px"); $(this).css("height", "500px"); } else { $(this).css("width", "75px"); $(this).css("height", "75px"); } }); }); 

Try something like: 尝试类似:

$('.option').on("click", function() {        
    $(this).addClass( "isActive" );
    $(this).css( "width", "500px" );
    $(this).css( "height", "500px" );
    $(this).css( "background-color", "#FFB2B2" );
    $(this).css( "background-color", "#FFB2B2" );
});

$('.isActive').on("click", function() {
    $(this).removeClass( "isActive" );
    $(this).removeAttr( "style" );
});

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

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