简体   繁体   English

JQuery 切换 Class 每次点击 1 个切换

[英]JQuery Toggle Class 1 toggle per click

I have this great reference here .我在这里有很好的参考。 I'm trying to toggle a class and will affect 1 at a time.我正在尝试切换 class 并且一次会影响 1 个。 But it seems to be not working without using a parent element to click just like in the reference.但是,如果不使用父元素单击,就像在参考中一样,它似乎无法正常工作。 I want to use only 1 class with a click function and will toggle effect one at a time.我只想使用 1 个 class 并单击 function 并一次切换一个效果。 Would be this possible?这可能吗?

 $(document).ready(function() { getQuoteButtton(); }); function getQuoteButtton() { // button quote $("body").on("click", ".btn-quote", function(e) { var $this = $(this); $this.toggleClass('quote-selected'); if ($this.hasClass('quote-selected')) { $this.text('Selected'); } else { $this.text('Select This Quote'); } }); }
 .section-block-table { width: 100%; margin: 0 auto; position: relative; border-collapse: separate; color: #2E384D; border-spacing: 0; }.btn-quote { background: #fff; position: relative; text-align: center; color: #49CD96; font-size: 14px; font-weight: 600; line-height: 20px; padding: 8px 9px; border-radius: 6px; border: 1px solid #49CD96; min-width: 166px; }.btn-quote:hover { background: #49CD96; color: #fff; }.btn-quote.quote-selected { background: #49CD96; color: #fff; }.btn-quote.quote-selected:before { content: "\f00c"; font-family: 'Font Awesome 5 Free'; color: #fff; margin-right: 7px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <table class="section-block-table"> <tr> <td><button class="btn-quote m-auto">Select This Quote</button></td> <td><button class="btn-quote m-auto">Select This Quote</button></td> <td><button class="btn-quote m-auto">Select This Quote</button></td> </tr> </table>

It is recommended to delegate.建议委托。 Why do you not want to use the parent element?为什么不想使用父元素?

Note I added a recommended tbody and gave it an ID注意我添加了一个推荐的 tbody 并给了它一个 ID

 $(function() { getQuoteButtton() }) function getQuoteButtton() { const $tb = $('#tb'); $tb.on('click', '.btn-quote', function(e) { $this = $(this); $this.toggleClass('quote-selected').text($this.hasClass('quote-selected')? 'Selected': 'Select This Quote'); $('.btn-quote', $tb).not(this).removeClass('quote-selected').text('Select This Quote'); }); }
 .section-block-table { width: 100%; margin: 0 auto; position: relative; border-collapse: separate; color: #2E384D; border-spacing: 0; }.btn-quote { background: #fff; position: relative; text-align: center; color: #49CD96; font-size: 14px; font-weight: 600; line-height: 20px; padding: 8px 9px; border-radius: 6px; border: 1px solid #49CD96; min-width: 166px; }.btn-quote:hover { background: #49CD96; color: #fff; }.btn-quote.quote-selected { background: #49CD96; color: #fff; }.btn-quote.quote-selected:before { content: "\f00c"; font-family: 'Font Awesome 5 Free'; color: #fff; margin-right: 7px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <table class="section-block-table"> <tbody id="tb"> <tr> <td><button class="btn-quote m-auto">Select This Quote</button></td> <td><button class="btn-quote m-auto">Select This Quote</button></td> <td><button class="btn-quote m-auto">Select This Quote</button></td> </tr> </tbody> </table>

To answer to your question, "it seems to be not working without using a parent element to click just like in the reference", it can be done, like below.要回答您的问题,“如果不使用父元素单击,就像在参考中一样,它似乎无法正常工作”,可以这样做,如下所示。

The strength of event delegation comes from the fact that it allows to listen to an event on elements created at runtime, as the event will bubble up to the element the listener is attached to.事件委托的强大之处在于它允许监听在运行时创建的元素上的事件,因为该事件将冒泡到监听器所附加的元素。 In other words, attaching the listeners directly to the button will work if and only if all the buttons needed during the app lifetime are already present in the dom when the listener is declared.换句话说,当且仅当在声明侦听器时,应用程序生命周期中所需的所有按钮都已经存在于 dom 中时,将侦听器直接附加到按钮才会起作用。

If that is not the case, and a button is created by javascript after dom content is loaded, delegating to an ancestor element is the solution.如果不是这种情况,并且在加载 dom 内容后由 javascript 创建一个按钮,则委托给祖先元素是解决方案。

mplungjan's answer is more robust, as a general solution, there are definitely cases for both imho mplungjan 的回答更稳健,作为一个通用解决方案,恕我直言,这两种情况肯定都有

 $(document).ready(function() { $('.btn-quote').click(toggleSelected) }) function toggleSelected(event) { // as we are listening to an event // event.target is the element that // was clicked by the user $clickedButton = $(event.target) $otherButtons = $(".btn-quote").not(event.target) $otherButtons.removeClass('quote-selected').text('Select This Quote') $clickedButton.toggleClass('quote-selected').text( $clickedButton.hasClass('quote-selected')? 'Selected': 'Select This Quote' ) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <table class="section-block-table"> <tbody> <tr> <td><button class="btn-quote m-auto">Select This Quote</button></td> <td><button class="btn-quote m-auto">Select This Quote</button></td> <td><button class="btn-quote m-auto">Select This Quote</button></td> </tr> </tbody> </table>

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

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