javascript语言
我想对一些span进行一些操作如下比如
<span id = "jack" onmouseover = "color" onmouseout = "uncolor">
就是要鼠标移到这个span上面就变黄,移开就变另外一个颜色。
但是我想在很多span里面实现,所以document.getElementById("jack")里面的参数就应该是动态的了,不知道该怎么实现。。求大神帮助
function color(){
document.getElementById("jack").style.background = "yellow";
}
function uncolor(id){
document.getElementById("jack").style.background = "AliceBlue";
}还有一个问题:怎么样能动态创建span?并且在动态创建的span里面也调用这些函数?谢谢。
我想对一些span进行一些操作如下比如
<span id = "jack" onmouseover = "color" onmouseout = "uncolor">
就是要鼠标移到这个span上面就变黄,移开就变另外一个颜色。
但是我想在很多span里面实现,所以document.getElementById("jack")里面的参数就应该是动态的了,不知道该怎么实现。。求大神帮助
function color(){
document.getElementById("jack").style.background = "yellow";
}
function uncolor(id){
document.getElementById("jack").style.background = "AliceBlue";
}还有一个问题:怎么样能动态创建span?并且在动态创建的span里面也调用这些函数?谢谢。
解决方案 »
- vs2008智能提示jquery问题
- 在地址拦运行js与在页面里运行js有啥区别?我这有个实例,请说明一下
- javascript有没有键值对类似hashtable?
- 为什么我用getElementById了,不能获得其下的input
- 关于使用AlphaImageLoader装载png透明图的问题--急.
- DHTML的对象为什么会变?
- 求高手,写个循环
- 怎样在javascript中定义和使用checkbox数组啊?
- 如何用JS代码禁止掉IE栏中查看源代码的功能,注意不是右键里的查看源代码!谢谢!
- javascript函数问题
- 如何实现简单带有指向点击处箭头的说明窗口
- 用CompareValidator控件判断2次输入密码是否一致,但始终是返回密码输入不一致,求解?
<script type="text/javascript">
function color(obj){
obj.style.background = "yellow";
}
function uncolor(obj){
obj.style.background = "AliceBlue";
}
</script>
<span onmouseover = "color(this)" onmouseout = "uncolor(this)">AAA</span>
<span onmouseover = "color(this)" onmouseout = "uncolor(this)">BBB</span>
<span onmouseover = "color(this)" onmouseout = "uncolor(this)">CCC</span>
<span onmouseover = "color(this)" onmouseout = "uncolor(this)">DDD</span>
var spans=document.getElementsByTagName("span");
for(var i=0;i<spans.length;i++){
spans[i].onmouseover=function(){
this.style.backgroundColor='yellow';
}
spans[i].onmouseout=function(){
this.style.backgroundColor='white';
}
}
}
window.onload=init;
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
span{width:20px;height:20px;display:block;float:left;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("span").hover(function(){
$(this).css("background","#333");
},function(){
$(this).css("background","#666");
});
});
</script>
</head>
<body>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</body>
</html>
你想要的应该是这种.
var dataContainer = $('<div id="dataContainer"></div>');
$.each(datas,function(index,data){
var s = $('<span>'+data+'</span></br>');
s.hover(function(){
$(this).addClass('yellow');
},function(){
$(this).removeClass('yellow');
});
dataContainer.append(s);
});
$('body').append(dataContainer);js生成的dom,用jquery,
http://jsfiddle.net/TbvMx/
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<style type="text/css">
#aa span {
background-color: #e8e8e8;
}
#aa span:hover {
background-color: #ffff00;
}
</style>
</head><body>
<div id="aa">
<span>aaaaaaaaa</span>
<span>566666666</span>
<span>aaaaaaaaa</span>
<span>454554544</span>
<span>aaaaaaaaa</span>
<span>aaaaaaaaa</span>
</div>
</body>
</html>