简体   繁体   English

通过超链接更改div背景颜色

[英]Changing a div background color through a hyperlink

Hello Stackoverlowers! 你好Stackoverlowers!

I need to sort out my div's and ensure that the background color of a div changes when a certain hyperlinks are closed. 我需要整理我的div并确保在关闭某些超链接时div的背景颜色发生变化。

My JS mark up is: 我的JS标记是:

<script>
$("bgcolor").click(function(){
    $("#blackandwhite").animate(
        {backgroundColor: '#000000'},
        "slow");}
);
}

My HTML is: 我的HTML是:

<div id="blackandwhite"> text here </div>

My CSS is: 我的CSS是:

#blackandwhite { width:100%; #FFFFFF; }

The link structure I am using to trigger the div to change color is like this: 我用来触发div改变颜色的链接结构是这样的:

<li><a href="#secondpagename" class="bgcolor"> link text </a></li>

Really cannot work out why it is not working and will appreciate any and all help! 真的无法解决为什么它不工作,并将欣赏任何和所有的帮助! I feel like I am almost there. 我觉得我快到了。

All I want to do is to have the hyperlink change the div background. 我想要做的就是让超链接更改div背景。 This hyperlink is also and will remain outside of the div. 此超链接也将保留在div之外。 Any ideas? 有任何想法吗?

There are two problems you need to address 您需要解决两个问题

  1. The selector needs to look for the class bgcolor and hence should be .bgcolor 选择器需要查找类bgcolor ,因此应该是.bgcolor
  2. Probably need to prevent the default action of the hyperlink via e.preventDefault() . 可能需要通过e.preventDefault()来阻止超链接的默认操作。 Not 100% sure about your intent here but if it's just to change the color and not jump in the page then this is likely what you want. 不是100%肯定你的意图,但如果它只是改变颜色而不是跳进页面,那么这很可能是你想要的。

Try the following instead 请尝试以下方法

$(".bgcolor").click(function(e) {
    $("#blackandwhite").animate(
        {backgroundColor: '#000000'},
        "slow");
    e.preventDefault()
});

EDIT 编辑

As others have pointed out jQuery doesn't support animation out of the box. 正如其他人指出的那样,jQuery不支持开箱即用的动画。 It does support direct changes of the color though via css 它确实支持通过css直接改变颜色

$("#blackandwhite").css(backgroundColor: '#000000');

If you really want color animation though there are a number of plugins available. 如果你真的想要彩色动画,虽然有很多插件可用。 For example 例如

You have forgotten the dot: .bgcolor 你忘记了点: .bgcolor

$(".bgcolor").click(function(){
    $("#blackandwhite").animate(
        {backgroundColor: '#000000'},
        "slow");}
);
}

Apart from that, I think it should work. 除此之外,我认为它应该有效。

jQuery didn't support color animation "out of the box". jQuery不支持“开箱即用”的彩色动画。 Only "numeric" properties can be animated. 只能为“数字”属性设置动画。 You still can set you background color "directly", without an animation. 您仍然可以“直接”设置背景颜色,而无需动画。

$(".bgcolor").click(function(){
    $("#blackandwhite").css(backgroundColor: '#000000');
});

If you are still want to animate color, you can try this plugin: http://www.bitstorm.org/jquery/color-animation/ 如果您仍想要为颜色设置动画,可以试试这个插件: http//www.bitstorm.org/jquery/color-animation/

Your select needs to be prefixed with a dot to pick elements that have the matching class instead of matching tag name: 您的选择需要以点为前缀,以选择具有匹配类而不是匹配标记名称的元素:

$(".bgcolor").click(function(){
    $("#blackandwhite").animate(
        {backgroundColor: '#000000'},
        "slow");}
);
}

The identifier for your click function is incorrect. 点击功能的标识符不正确。 It should be: 它应该是:

$(".bgcolor").click(function(){
);

Note the dot to represent that it is searching for a class. 注意点表示它正在搜索类。 Also the script needs to either be placed after the element, or in document.ready() tags. 此外,脚本需要放在元素之后,或放在document.ready()标记中。

First you are trying to select by css class. 首先,您尝试通过css类进行选择。 Correct selector syntax for this is .<classname> . 正确的选择器语法是.<classname> So your selector for binding the click event should be: 所以你的选择器绑定click事件应该是:

$(".bgcolor")

jQuery selectors jQuery选择器


Beware that jQuery cannot animate colors out of the box, you have to use the Color plugin, or to use jQuery UI which overloads the animate() method to support animating colors. 请注意,jQuery 无法为开箱即用的颜色 设置动画 ,您必须使用Color插件,或使用重载animate()方法的jQuery UI来支持动画颜色。

From the .animate() documentation : .animate()文档

... most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width, height, or left can be animated but background-color cannot be , unless the jQuery.Color() plugin is used)/// ... 大多数非数字属性无法使用基本的jQuery功能进行动画处理 (例如,宽度,高度或左边可以设置动画,但背景颜色不能 ,除非使用jQuery.Color()插件)// /

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

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