1、首先,在html中设置一个p1和一个隐藏域:<p1 onclick="fun5(this);">点击文本!</p1> <input type="hidden" id="hidden" value="blue">
2、接下来,定义函数fun5;fun5中应该传入一个参数,该参数可以取任一名: <script type="text/javascript"> function fun5(a) { var hide = document.getElementById("hidden"); if (hide.value == "blue") { alert("bianhong"); a.style.color = "red"; hide.value = "red" } else if (hide.value == "red") { alert("bianlan"); a.style.color = "blue"; hide.value = "blue" } }</script>
3、完整代码如下:<!D蚪嬷吃败OCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript"> var x = "global";</script><style type="text/css">p1 { color: blue;}</style></head><body> <p1 onclick="fun5(this);">点击文本!</p1> <input type="hidden" id="hidden" value="blue"> <script type="text/javascript"> function fun5(a) { var hide = document.getElementById("hidden"); if (hide.value == "blue") { alert("变红"); a.style.color = "red"; hide.value = "red" } else if (hide.value == "red") { alert("变蓝"); a.style.color = "blue"; hide.value = "blue" } } </script></body></html>