jquery 动态更改 div 背景色?

jquery 动态更改 div 背景色?,第1张

用jquery如何实现点击一栏目实现栏目变色,再点击另一栏目也变色,但原来的变回原色,有个数组a(1,2,3)循环遍历输出a的值。jquery 动态更改 div 背景色代码如下:

<head>

<script type="text/javascript" src="/js/jQuery.js"></script>

<script type="text/javascript">

$(".test").onclick = function(){

$(this).css({"background": "red"})

}

</script>

</head>

<body>

<div class="test"

style="background-color: graywidth: 300pxheight: 200px"></div>

</body>

</html>$(".test").onclick = function(){

$(this).css({"background": "red"})

}

改为:

$(".test").click = function(){

$(this).css({"background-color": "red"})

}

扩展资料:

jQuery是一个快速、简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架)。jQuery设计的宗旨是“write Less,Do More”,即倡导写更少的代码,做更多的事情。它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档 *** 作、事件处理、动画设计和Ajax交互。

2. jQuery的核心特性可以总结为:具有独特的链式语法和短小清晰的多功能接口;具有高效灵活的css选择器,并且可对CSS选择器进行扩展;拥有便捷的插件扩展机制和丰富的插件。jQuery兼容各种主流浏览器,如IE 6.0+、FF 1.5+、Safari 2.0+、Opera 9.0+等。

3.jquery所有的事件绑定都没有on这个关键字。另外,jquery的事件绑定需要放到readyfunction中去。具体来说:

<head>

<script type="text/javascript" src="/js/jQuery.js"></script>

<script type="text/javascript">

$(document).ready(function() {

$(".test").click(function() {

$(this).css({"background-color": "red"})

})

})

</script>

</head>

<body>

<div class="test"

style="background-color: graywidth: 300pxheight: 200px"></div>

</body>

</html>

参考资料:jQuery_百度百科

1、新建一个html文件,命名为test.html。

2、在test.html文件内,使用p标签创建一个区域,下面将在p标签内添加一个a标签。

3、在test.html文件内,设置p标签的id为mytest,主要用于下再通过该id获得p标签对象。

4、在test.html文件内,使用button标签创建一个按钮,按钮名称为“添加一个a标签”。

5、在test.html文件内,给button按钮绑定onclick点击事件,当按钮被点击时,执行addaa()函数。

6、在js标签中,创建addaa()函数,在函数内,创建一个变量html保存要添加的a标签,再使用append()方法向被选对象(p标签)内添加一个a标签。

jquery 的animate()方法是不可以改变背景颜色的,如果想改变背景颜色,需要引入jquery.color插件,再用animate()来实现背景色变化,具体代码如下:

<script src='jquery.animate-colors.js'></script>

$("div:contains('你好')").click(function(){$(this).animate({background:"red"},3000)})

下面是实现背景色改变的结果:

扩展资料

jquery改变背景颜色的动态方法

1、脚本方法:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script

<select class="rez"> <option value="Not Confirmed">Not Confirmed</option>

2、选定方法:

<option value="Confirmed" selected="selected">Confirmed</option></select><select class="rez">

<option value="Not Confirmed" selected="selected">Not Confirmed</option>

<option value="Confirmed">Confirmed</option></select>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11570502.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存