1、新建一个html文件,命名为test.html。
2、在test.html文件内,创建一个div模块,设置其class属性为bg,id属性为mydiv,用于下面设置css样式和获取div对象。
3、在test.html文件内,使用css设置div的样式,设置其宽度为500px,高度为272px,背景图片为2.jpg。
4、在test.html文件内,在div的下面创建一个button按钮,按钮名称为“更换背景颜色”。
5、给button按钮绑定onclick点击事件,当按钮被点击时,执行myfun()函数。
6、在test.html文件内,在js标签内,创建myfun()函数,在函数内,使用getElementById()方法通过id获得div对象,设置对象中的backgroundImage背景属性为另一张图片,从而实现改变div的背景图片。就完成了,运行就可以了。
很简单的:如果你是在学习JS、遇到了各种问题,那么你一定要来这个企鹅裙,前面前面是二九六,中间是五九一,最后面就是二九0,来这里可以得到专人解答,期待你的加入!!!
<div id="txt">我是要改变的文字</div>
<script>
//首先咱们找到要改变的文字
var txt = document.getElementById("txt")
//然后设置一个标记flag
var flag = true
//然后点击这个div或者按钮
txt.onclick = function(){
//改变里面文字颜色
if(flag){
this.style.color = "red"
flag = false
}else{
this.style.color = "#000"
flag = true
}
}
</script>
本文实例讲述了js实现选中复选框文字变色的方法。分享给大家供大家参考。具体如下:这里实现选中复选框时,文字加上一个背景色,变通一下,还是很有用的。
运行效果如下图所示:
在线演示地址如下:
http://demo.jb51.net/js/2015/js-checkbox-cha-font-color-codes/
具体代码如下:
<html>
<head>
<title>选中复选框文字变色</title>
<style>
.checkbox
{
background-Color:e-xpression(this.checked?'yellow':'buttonface')
}
</style>
</head>
<script>
function
chaCloor(field){
var
pig
=
field.checked
pig?field.nextSibling.style.backgroundColor="skyblue":field.nextSibling.style.backgroundColor="white"
}
</script>
<body>
<table
height=48
width=136>
<tr>
<td><input
type="checkbox"
onClick="chaCloor(this)"><span>网页特效</span></td>
</tr>
<tr>
<td><input
type="checkbox"
onClick="chaCloor(this)"><span>源码下载</span></td>
</tr>
<tr>
<td><input
type="checkbox"
onClick="chaCloor(this)"><span>编程软件</span></td>
</tr>
</table>
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。