简体   繁体   English

jQuery根据单击按钮的次数更改元素

[英]jQuery changing elements depending on how many times the button has been clicked

I don't know if I just don't know how to use .toggle() or if I'm just writing the .click() function wrong but essentially I want two different things to happen when I click a button, depending on if it is toggled on or not. 我不知道如果我只是不知道如何使用.toggle()或者如果我只是写了.click()当我点击一个按钮取决于功能失常,但基本上我想要两个不同的事情发生,是否打开。

Basically if the button is clicked, I want the hidden div to show and I want the text of the button to change. 基本上,如果单击该按钮,则希望显示隐藏的div,并且希望更改按钮的文本。 Then when it's clicked again, the div is hidden again and the text changes back to what it was previously 然后,当再次单击它时,div将再次隐藏,文本将恢复为之前的状态

 $(document).ready( function() { var count = 0; $("#dropdown-toggle-button").click(function() { count++; if (count % 2 !== 0) { // on odd clicks do this $('#dropdown-column').css('display', 'block'); $('#dropdown-toggle-button').text('Toggled Button'); } else if (count % 2 === 0) { // on even clicks do this $('#dropdown-column').css('display', 'none'); $('#dropdown-toggle-button').text('Un-toggled Button'); }; }); }); 
 #dropdown-column { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <h2 id="sample-header">Header</h2> <a href="#" id="dropdown-toggle-button">Un-toggled Button</a> <div id="dropdown-column"> <p>Hello, Stranger</p> </div> 

Your only problems are in fact syntax errors! 您唯一的问题实际上是语法错误!

First off, you have forgotten to add the extra }); 首先,您忘记添加额外的}); to close off the outside document.ready function. 关闭外部document.ready功能。

Next, you have a semicolon before else if. 接下来,如果没有,则使用分号。 This tells javascript that it's starting to read another statement rather than continuing to read the if statement from before, which means "else" doesn't know what to connect itself to. 这告诉javascript,它正在开始读取另一条语句,而不是继续从之前读取if语句,这意味着“ else”不知道将自身连接到什么。

If you want to refine your code a little bit more, the trailing if statement after the else is redundant, if there are only 2 states, then you can just do 如果您想进一步完善代码,则else后面的if语句是多余的,如果只有2个状态,则可以

if(statement passes as true){
    // do stuff
}
else { 
    // your code here 
}

Looks like the paren/curly brace matching was a bit off in your javascript. 看起来在您的JavaScript中,括号/弯括号匹配有点偏离。 Try this: 尝试这个:

$(document).ready( function() {
  var count = 0;
  $("#dropdown-toggle-button").click(function() {
    count++;

    if (count % 2 !== 0) {
      $('#dropdown-column').css('display', 'block');
      $('#dropdown-toggle-button').text('Toggled Button');
    }
    else {
      $('#dropdown-column').css('display', 'none');
      $('#dropdown-toggle-button').text('Un-toggled Button');
    }
  });
});

According to jQuery docs, ( http://api.jquery.com/toggle/ ) 根据jQuery文档,( http://api.jquery.com/toggle/

With no parameters, the .toggle() method simply toggles the visibility of elements: 没有参数,.toggle()方法仅切换元素的可见性:

$( ".target" ).toggle();

The matched elements will be revealed or hidden immediately, with no animation, by changing the CSS display property. 通过更改CSS显示属性,匹配的元素将立即显示或隐藏,而不会产生动画。 If the element is initially displayed, it will be hidden; 如果最初显示该元素,则它将被隐藏; if hidden, it will be shown. 如果隐藏,它将显示。 The display property is saved and restored as needed. display属性将根据需要保存和恢复。 If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline. 如果一个元素的显示值为inline,则将其隐藏并显示,它将再次以inline显示。

Thus, although the code you've written should work just fine, it is not a very clean solution. 因此,尽管您编写的代码应该可以正常工作,但这并不是一个很干净的解决方案。 I'd suggest the following, as to use the jquery built-in .toggle function. 我建议使用jquery内置的.toggle函数。

$(document).ready( function() {

    $("#dropdown-toggle-button").click(function() {

        // first toggle the display of the column element
        $('#dropdown-column').toggle();

        // then check for its display status
        // http://api.jquery.com/visible-selector/
        if ($('#dropdown-column').is(':visible')) {
            // display: block
            $('#dropdown-toggle-button').text('Toggled Button');

        } else {
            // display: none
            $('#dropdown-toggle-button').text('Un-toggled Button');
        }



    });
});

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

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