读书人

小弟我想做个按钮点他他变黑再点他一次

发布时间: 2012-04-11 17:42:33 作者: rapoo

我想做个按钮点他他变黑再点他一次他变白再点一次又变黑这样反复 用js或者jq怎么做
这个想了好久不回 我是个初学者~~~ 一定要是js或者jq的代码啊

[解决办法]
<script type="text/javascript">
function change(){
var a=document.getElementById("test");
var t=a.getAttribute("num");
if(t=="1"){
a.style.backgroundColor="black";
a.style.color="white";
}else{
a.style.backgroundColor="white";
a.style.color="black";
}
a.setAttribute("num",-t);
}
</script>
</head>

<body>
<input type="button" num="1" id="test" style="background-color:#FFF; color:#000" value="test" onclick="change()">
</body>
这样试试
[解决办法]

HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><style type="text/css">input.white { background-color:white; }input.black { background-color:black; color:white; }</style><script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script><script type="text/javascript">$(document).ready( function() {    $("#btn").addClass('white').toggle(function() { $(this).removeClass("white").addClass("black"); }, function() { $(this).removeClass("black").addClass("white"); });});</script></head><body><input type="button" id="btn" value="我是按钮" /></body></html> 

读书人网 >JavaScript

热点推荐