简体   繁体   English

我无法使用jquery选择字体真棒元素

[英]I can't select font-awesome element using jquery

COMPLETE CODE IN FIDDLE LINE 36 JAVASCRIPT IS ISSUE PART https://jsfiddle.net/ybp7gtup/7/ 在第36行中完成代码JAVASCRIPT是发行部分https://jsfiddle.net/ybp7gtup/7/

I want to change the color of the icon using js or jquery as I am using form validation to change color on user behaviour. 我想使用js或jquery更改图标的颜色,因为我正在使用表单验证来更改用户行为的颜色。 I can't seem to select the <i> in the DOM, can anyone advise? 我似乎无法在DOM中选择<i> ,有人可以建议吗?

html: HTML:

<div class="col-xs-1 col-md-1 icons-div">
        <div class="row">
            <i class="fa fa-user fa-color"></i>
         </div>
 </div>

CSS: CSS:

select:invalid {
    outline: none;
    box-shadow: 0 0 5px red;
}

select:valid {
    outline: none;
    box-shadow: 0 0 5px #449d44;
}

.fa {
    font-size: 3em;
}

.fa-calendar {
    font-size: 2.5em;
}

.hide-div .fa {
    font-size: 1em;
}

.red {
    color: #FFA5A5;
}

.green {
    color: #449d44;
}

jquery: jQuery的:

$('.fa-color').addClass('red');

Your assumption that the jQuery is not working is wrong. 您认为jQuery无法正常工作的假设是错误的。 Your problem is that your selector is (most likely) weaker than the one applying the color property on the element when it has both fa-color and red classes. 您的问题是,当选择器同时具有fa-colorred类时,选择器(最有可能)比在元素上应用color属性的选择器弱。 This should work: 这应该工作:

.fa-color.red {
    color: #FFA5A5;
}

Try with to edit the fa-color class ( no need to use jquery ) 尝试编辑fa-color类(无需使用jquery)

.fa-color{
   color: #FFA5A5 !important;
}

Just add an ID. 只需添加一个ID。 and

<div class="col-xs-1 col-md-1 icons-div">
    <div class="row">
        <i class="fa fa-user" id = "color_guy"></i>
     </div>
</div>

then with jQuery: 然后用jQuery:

$(document).ready(function() {
    $('#color_guy').addClass('red');
)};

This should work! 这应该工作!

It was weird, but jquery would randomly not select certain elements (or that is what appeared to be happening). 这很奇怪,但是jquery不会随机选择某些元素(或者这似乎正在发生)。 Regular js would select the element, and a few times I just plowed on, but in one case I really wanted to use jQuery's toggle(). 普通的js会选择该元素,而我只是几次尝试,但是在一种情况下,我真的很想使用jQuery的toggle()。 I couldn't even figure out how to search for an answer until it dawned on me that it wasn't random at all, but always font awesome classes where jQuery failed (but produced no errors). 我什至不知道如何搜索答案,直到我意识到这根本不是随机的,但是在jQuery失败的情况下,字体总是很棒(但不会产生错误)。

After struggling all day with this, I stumbled across this article that seems to address this issue. 经过一整天的努力后,我偶然发现了这篇似乎可以解决此问题的文章。 It sounds like font awesome is affecting something in the element, which makes the jquery statements not work as expected. 听起来真棒字体正在影响元素中的某些内容,这使jquery语句无法按预期方式工作。 I believe the work-arounds given in the article are going to work, but i haven't tested them yet. 我相信本文中给出的解决方法将起作用,但是我尚未对其进行测试。

https://fontawesome.com/how-to-use/on-the-web/using-with/jquery https://fontawesome.com/how-to-use/on-the-web/using-with/jquery

ABE: So, the article says that the font-awesome script seeks out the tags with the fontawesome classes (fas, fa, etc) and REPLACES them with , so it suggested adding an extra js reference that would cause fontawesome to NEST the tags inside the original tags. ABE:所以,文章说font-awesome脚本使用fontawesome类(fas,fa等)查找标签,并用替换它们,因此建议添加一个额外的js引用,这将导致fontawesome将其标签嵌套在内部原始标签。 Sounded like a winner, but it didn't work for me. 听起来像赢家,但这对我没有用。

Then I found THIS article on fontawesome's site that shows how to use fontawesome via css pseudoelements, and I thought that would work, but it did not. 然后,我在fontawesome的站点上找到了这篇文章,该文章显示了如何通过CSS伪元素使用fontawesome,我认为这样做可以,但是没有用。

Frankly, at this point, I'm ready to just use icon images. 坦白说,在这一点上,我准备使用图标图像。 I'm not very good at javascript or jquery and this is way more complicated than it seems like it should be. 我不太擅长javascript或jquery,这比看起来应该要复杂得多。

* UPDATE * *更新*

I was not preceding the class name selector with a "." 我没有在类名选择器前添加“。”。 in the jQuery. 在jQuery中。 so, for example, I was writing $('icon') when I should have written $('.icon'). 因此,例如,当我应该写$('。icon')时,我正在写$('icon')。 Man, like a day and half wasted over a stupid dot. 男人,一天半浪费在一个愚蠢的点上。 EVERY method works fine if the class name selector is typed in correctly. 如果正确输入了类名选择器,则每个方法都可以正常工作。

Tested your code in a jsfiddle, it works. 在jsfiddle中测试了您的代码,它可以工作。 You might have forgotten to include JQuery into your document header. 您可能已经忘记了将JQuery包含在文档标题中。 For example: <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> 例如: <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>

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

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