简体   繁体   English

jQuery触发类更改

[英]Jquery to trigger class change

Tad new to this jQuery thing. 对jQuery这个东西不熟悉。 This is the code that the software we use spits out and I cannot change the code. 这是我们使用的软件吐出的代码,我无法更改代码。 So I have to use jQuery to manipulate it to what I want. 因此,我必须使用jQuery将其操纵到所需的状态。

<div class="responseItem">

    <span class="response_text">yes</span>
    <div style="clear:both"></div>
</div>

<div class="responseItem">

    <span class="response_text">no</span>
    <div style="clear:both"></div>
</div>

When you select a option it will add a class called selected like this: 当您选择一个选项时,它将添加一个名为selected的类,如下所示:

<div class="responseItem selected">

    <span class="response_text">yes</span>
    <div style="clear:both"></div>
</div>

I added jQuery to get my icon I want like this as a default icon: 我添加了jQuery以使我想要的图标成为默认图标:

$("span.response_text").prepend('<i class="fa fa-circle-o">&nbsp;</i>');

So your code is now like this when viewed in browser: 因此,现在在浏览器中查看时,您的代码如下所示:

<div class="responseItem">
   <span class="response_text">
      <i class="fa fa-circle-o"> </i>
        yes
   </span>
   <div style="clear:both"></div>
</div>

It will give you a permanent circle icon which is perfect. 它将为您提供一个完美的永久圆圈图标。 I now want it to change icons dependant on if it is selected or not. 我现在希望它根据是否选择来更改图标。 So the default will be fa-circle-o and when selected it will change to fa-dot-circle-o 因此,默认值将为fa-circle-o,并且在选择时将其更改为fa-dot-circle-o

$("span.response_text").click(function(){
    $("i").removeClass("fa-circle-o").addClass("fa-dot-circle-o");
});

This kind of works but then changes both icons to this so I am kind of getting there but not sure if the .click is the right way to go? 这种工作方式,然后将两个图标都更改为此,所以我有点到达那里,但不确定.click是否是正确的方法?

I basically need it to change the icon class as soon as the div class responseItem selected event happens. 我基本上需要它在div类responseItem selected事件发生时立即更改图标类。

Any help will be so much appreciated. 任何帮助将不胜感激。

Thank you 谢谢

If I understand correctly, there are multiple items on the page with the <i> tag. 如果我理解正确,则页面上有多个带有<i>标记的项目。 Therefore I'd suggest: 因此,我建议:

$("i.fa-circle-o").removeClass("fa-circle-o").addClass("fa-dot-circle-o");

This will ensure it only replaces the 'selected' item. 这将确保它仅替换“选定”项。

Otherwise, you will have to alter your $("i") selector to something more specific, as it will hit every <i> on the page. 否则,您将不得不将$("i")选择器更改为更具体的选择器,因为它会命中页面上的每个<i>

Here is a JS fiddle demonstrating a more complete solution: 这是一个JS小提琴,展示了更完整的解决方案:

http://jsfiddle.net/2otd1mh5/ http://jsfiddle.net/2otd1mh5/

$(document).ready(function () {
    $("span.response_text").prepend('<i class="fa fa-circle-o">&nbsp;</i>');
});

$("span.response_text").click(function(){
    var dotItems = $('i.fa-dot-circle-o');

    $(this).find('i').removeClass("fa-circle-o").addClass("fa-dot-circle-o");

    dotItems.removeClass("fa-dot-circle-o").addClass("fa-circle-o");
});

After adding the class selected to the div.responseItem , manually raise an custom event as per the following code snippet. selected的类添加到div.responseItem ,按照以下代码片段手动引发自定义事件。

Code snippet: 程式码片段:

$("div.responseItem").addClass("selected").trigger('classChanged');

And define the custom classChanged event for div.selected like this to change your icon's class. 并为div.selected定义自定义classChanged事件,以更改图标的类。

$(document).on('classChanged',"div.selected", function() {
     $("i").removeClass("fa-circle-o").addClass("fa-dot-circle-o");
});

Hope this helps you! 希望这对您有所帮助!

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

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