jQuery的on和off事件,但是同一个按钮

huangapple go评论56阅读模式
英文:

jQuery on and off events but same button

问题

我想要制作一个按钮,当按下时,会改变我的页面的背景颜色。然后,当再次按下它时,页面会恢复到原来的颜色。我不明白如何再次将其恢复到原始颜色(使用切换开关)。

---

$(function () {
$("#button").click(function () {
$("p").css("background-color", "red");
});
});


<details>
<summary>英文:</summary>

I want to make a button that, when pressed, changes the background color of my page. Then, when you press it again, the page will go back to its original color. I don&#39;t understand how to make it go back to the original color again (using Toggle switch).

---


$(function () {
$("#button").click(function () {
$("p").css("background-color", "red");
});
});


</details>


# 答案1
**得分**: 0

理想情况下,你可以使用*类*来实现这一点,这样可以轻松切换。例如:

```html
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言: lang-js -->
$(function () {
  $("#button").click(function () {
    $("p").toggleClass("red");
  });
});

<!-- 语言: lang-css -->
.red {
  background-color: red;
}

<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>测试</p>
<button id="button" type="button">点击</button>

<!-- 结束代码片段 -->

如果必须使用直接样式规则,一种方法是将原始值存储在某个地方。然后在单击处理程序中,只需将当前值与原始值进行比较,然后以某种方式进行更改。例如:

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言: lang-js -->
$(function () {
  const original = $("p").css("background-color");
  
  $("#button").click(function () {
    const current = $("p").css("background-color");
    if (current === original) {
      $("p").css("background-color", "red");
    } else {
      $("p").css("background-color", original);
    }
  });
});

<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>测试</p>
<button id="button" type="button">点击</button>

<!-- 结束代码片段 -->
英文:

Ideally you would use classes for this, which are easily toggled. For example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(function () {
  $(&quot;#button&quot;).click(function () {
    $(&quot;p&quot;).toggleClass(&quot;red&quot;);
  });
});

<!-- language: lang-css -->

.red {
  background-color: red;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;p&gt;Test&lt;/p&gt;
&lt;button id=&quot;button&quot; type=&quot;button&quot;&gt;Click&lt;/button&gt;

<!-- end snippet -->


If you must use direct style rules then one approach is to store the original value somewhere. Then in the click handler just compare the current value with that original and change it one way or the other. For example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(function () {
  const original = $(&quot;p&quot;).css(&quot;background-color&quot;);
  
  $(&quot;#button&quot;).click(function () {
    const current = $(&quot;p&quot;).css(&quot;background-color&quot;);
    if (current === original) {
      $(&quot;p&quot;).css(&quot;background-color&quot;, &quot;red&quot;);
    } else {
      $(&quot;p&quot;).css(&quot;background-color&quot;, original);
    }
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;p&gt;Test&lt;/p&gt;
&lt;button id=&quot;button&quot; type=&quot;button&quot;&gt;Click&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 04:24:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定