简体   繁体   English

jQuery单击更改多个元素的颜色

[英]jQuery changing colours of multiple elements on click

and thanks, beginner here. 谢谢,初学者在这里。

The formatting doesn't really work in jsfiddle, but that doesn't matter to my question. 格式实际上在jsfiddle中不起作用,但这与我的问题无关。

Here's what I have: http://jsfiddle.net/8X7ZE/1/ 这是我所拥有的: http : //jsfiddle.net/8X7ZE/1/

What I'm trying to do is change all the elements with the blue colour to red (the h1 text and navigation bar) when the red div is clicked. 我想做的是单击红色div时将所有具有蓝色的元素更改为红色(h1文本和导航栏)。

$(document).ready(function(){
    $('.colorred').click(function(){
        $('.over', '#thispage').css({"background-color", "#821122"});
        $('h1', '.nav').css({"color", '#821122'});
    });
});

Currently all that happens is it makes the elements I point to never appear (they fade in). 当前发生的所有事情都是使我指向的元素永不出现(它们淡入)。

You had issues with commas in selectors and css method parameters. 选择器和CSS方法参数中的逗号有问题。

This is your code: 这是您的代码:

$('.over', '#thispage').css({"background-color", "#821122"});
$('h1', '.nav').css({"color", '#821122'});

This is how I did it: 这是我的方法:

$('.over, #thispage').css("background-color", "#821122");
$('h1, .nav').css("color", '#821122');

So, if you want multiple selectors, just add commas, don't make two different parameters. 因此,如果要使用多个选择器,则只需添加逗号, 而不要使用两个不同的参数。

Then, the css(). 然后,css()。 As a parameter, you can either specify key, value , which I did, or an object, which you tried to do, but the correct syntax would be: 作为参数,您可以指定我做过的key, value或尝试做的对象,但是正确的语法是:

$('h1, .nav').css({"color": '#821122'});

And for multiple rules: 对于多个规则:

$('h1, .nav').css({"color": '#821122', "background-color": "#821122"});

See it working here: http://jsfiddle.net/8X7ZE/2/ 看到它在这里工作: http : //jsfiddle.net/8X7ZE/2/

you can just use JavaScript for this, there's no need to use JQuery. 您可以为此使用JavaScript,而无需使用JQuery。

I did a simple HTML colour changing project at college where you had to do the exact same thing. 我在大学里做了一个简单的HTML颜色更改项目,您必须做同样的事情。

<div class="javascriptcolours">
    <a href=javascript:void(0) onClick="changeBgcolor('#333333'); changeColor('#000000'); changeText('#ffffff'); changeBanner('images/banner.png'); changeHeader('images/cost_header.png'); changeMedia('images/cost_media.png'); changeLinkbox1('images/nav_plot.png'); changeLinkbox2('images/nav_director.png'); changeLinkbox3('images/nav_actors.png'); changeLinkbox4('images/nav_location.png'); changeLinkbox5('images/nav_cost.png'); changeFoot('#000000'); changeLinks('#808080');"><img src="Images/black.png" alt="Black" /></a>
    <a href=javascript:void(0) onClick="changeBgcolor('#999999'); changeColor('#ffffff'); changeText('#000000'); changeBanner('images/banner_white.png'); changeHeader('images/cost_header_black.png'); changeMedia('images/cost_media_white.png'); changeLinkbox1('images/nav_plot_white.png'); changeLinkbox2('images/nav_director_white.png'); changeLinkbox3('images/nav_actors_white.png'); changeLinkbox4('images/nav_location_white.png'); changeLinkbox5('images/nav_cost_white.png'); changeFoot('#ffffff'); changeLinks('#808080');"><img src="Images/white.png" alt="White" /></a>
    <a href=javascript:void(0) onClick="changeBgcolor('#a00000'); changeColor('#ff3332'); changeText('#ffffff'); changeBanner('images/banner_red.png'); changeHeader('images/cost_header.png'); changeMedia('images/cost_media_red.png'); changeLinkbox1('images/nav_plot_red.png'); changeLinkbox2('images/nav_director_red.png'); changeLinkbox3('images/nav_actors_red.png'); changeLinkbox4('images/nav_location_red.png'); changeLinkbox5('images/nav_cost_red.png'); changeFoot('#ff3332'); changeLinks('#ff9999');"><img src="Images/red.png" alt="red" /></a>
</div>

So basically that code above has the main images or divs which you're using... 所以基本上上面的代码具有您正在使用的主要图像或div ...

<img src="Images/red.png" alt="red" />

here is an example of the changeColor method in my JS file attached to the site, this grabs the three elements navigation, maininfo and mediaframe and puts then allows the colour to be passed in with the above code. 这是附加到该站点的JS文件中的changeColor方法的示例,该示例获取导航,maininfo和mediaframe这三个元素,然后将颜色与上面的代码一起传递。

function changeColor(colorIn)
    {
        document.getElementById("navigation").style.backgroundColor = colorIn;
        document.getElementById("maininfo").style.backgroundColor = colorIn;
        document.getElementById("mediaframe").style.backgroundColor = colorIn;
    }

as long as the onClick in the html code (top) and function in your JS file are the same, so in your case you'll probably want to call it changeBgcolor instead of changeColor. 只要html代码中的onClick(顶部)和JS文件中的函数相同,因此在您的情况下,您可能需要将其命名为changeBgcolor而不是changeColor。

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

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