简体   繁体   English

为什么jQuery.css()对我不起作用?

[英]Why is jQuery.css() not working for me?

I've been struggling with some code and quite seem to get it to work - apologies if this is a silly question, I'm still quite new to JavaScript/jQuery etc. 我一直在努力处理一些代码,并且似乎可以使它正常工作-抱歉,这是一个愚蠢的问题,对于JavaScript / jQuery等我还是很陌生。

I'm trying to build a Weather App as part of the Free Code Camp course and have built using simpleWeather.js. 我正尝试在Free Code Camp课程中构建Weather App,并已使用simpleWeather.js构建了该应用。 I'm now trying to animate a little bit of the page to make it a bit more interactive. 我现在正在尝试对页面进行动画处理,以使其更具交互性。 I have some code: 我有一些代码:

$('#drop').on('click', function() {
  $('.fa-chevron-circle-down').css('transform', 'rotate(180deg)');
});

I believe this should on a click, rotate the icon 180 degrees but no matter what variations I try, I can't seem to get it to work. 我认为应该单击一下,将图标旋转180度,但是无论我尝试什么变化,我似乎都无法正常使用。 After spending hours yesterday playing with it I decided it's time to ask for help! 在昨天花了几个小时玩完游戏之后,我决定是时候寻求帮助了! (The idea is that once it works, I'll animate the elements below so they only show when the button is clicked). (想法是,一旦它起作用,我将为下面的元素设置动画,以便它们仅在单击按钮时显示)。

My codepen is https://codepen.io/woftis/pen/VjppYM 我的代码笔是https://codepen.io/woftis/pen/VjppYM

Thanks in advance for any help anyone can give. 在此先感谢您提供任何帮助。

Your #drop element has been created after DOM load ie dynamically . 您的#drop元素是在DOM load后即动态创建的。 Hence you need event delegation here. 因此,您需要在这里进行event delegation So attach your click to document and then refer your element - #drop . 因此,将点击附加到document ,然后引用element - #drop

$(document).on('click','#drop', function() {
  $('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});

Updated PEN

To be more simpler, since #weather element existed during DOM load , insted of document you can add it to #weather element. 更简单地说,由于#weather元素是在DOM load期间存在的,因此您可以在document#weather元素。 Consider below example. 考虑下面的例子。

$('#weather').on('click','#drop', function() {
  $('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});

and also I believe, it should have been 180deg to be proper turn-around. 我也相信,应该适当地将其转过180deg

Updated PEN 2 with 180deg and #weather

I was having an issue with jquery .css function with code that's very similar to to OP. 我在使用与OP非常相似的代码的jquery .css函数时遇到问题。

Problem 问题

The .css jquery function would not change the css on my .dot-one or .dot-two classes when I clicked button with the #correct-email id. 当我单击带有#correct-email id的按钮时, .css jQuery函数不会更改.dot-one.dot-two类的css。

Here's the required code examples: 这是必需的代码示例:


$(document).on('click', '#correct-email', function () {
        $('.dot-one').css('background-color', '$white');
        $('.dot-two').css('background-color', '$gray-darker');
        ...
      });

.dot-one {
  background-color: $gray-darker;
}

.dot-two {
  background-color: $white;
}

Solution

$(document).on('click', '#correct-email', function () {
        $('.dot-one').css('background-color', '#fff');
        $('.dot-two').css('background-color', '#222');
        ...
      });

Apparently, jquery doesn't like variables SASS variables being passed into the .css function. 显然,jquery不喜欢将变量SASS变量传递给.css函数。 I couldn't find that anywhere in the documentation. 我在文档的任何地方都找不到。 My assumption is that it'd be able to compute the new value based off of the SASS variable. 我的假设是,它能够基于SASS变量来计算新值。 But it seems all that stuff is pre-processed by sass and not made available to css. 但是似乎所有这些东西都是由sass预处理的,而不是可供CSS使用的。

According to the SASS documentation this is what it sounds like is happening. 根据SASS文档,这就是听起来正在发生的事情。

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

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