JavaScript实现自动消除按钮功能演示例子,使用JavaScript自动消除前项显示的内容代码实例,在网页特定方位显示内容,第1个按钮可用,但第2个在第一个点击之后就不能用了,如果想让第2个按钮起作用,你需要对其进行功能消除,如本示例代码就能够实现这种功能。
运行效果如下图所示:
代码如下:
|
<!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"
xml:lang= "en" > <head> <meta http-equiv= "Content-Type"
content= "text/html;charset=UTF-8"
/> <title>JavaScript自动消除前项显示的内容</title> <style type= "text/css" > body{padding:20px;background: #333;} h1{color:white;} em{width:100px;height:100px;background: #EEE;border:1px #CCC solid;padding:10px;cursor:pointer;} div{width:100px;height:100px;background: #444;margin:10px;display:none;color:white;text-align:center;line-height:100px;} </style> </head> <body> <h1>鱼和熊掌不能兼得 - wei2008.com</h1> <br /> <em>我要鱼</em> <em>我要熊掌</em> <div id= "a" >鱼</div> <div id= "b" >熊掌</div> <script type= "text/javascript" > window.onload= function (){ var
abtn=document.getElementsByTagName( "em" )[0]; var
bbtn=document.getElementsByTagName( "em" )[1]; var
a=document.getElementById( "a" ); var
b=document.getElementById( "b" ); abtn.onclick= function (){ a.style.display= "block" ; bbtn.onclick= null ; } bbtn.onclick= function (){ b.style.display= "block" ; abtn.onclick= null ; } } </script> </body> </html> |