通过js给页面元素添加事件

王朝html/css/js·作者佚名  2008-12-13
宽屏版  字体: |||超大  

最近做一个登录仿XP多用户页面,要使用js给页面元素添加事件的方式去处理。网上G了很久找到一个比较简单的示例:view plaincopy to clipboardprint?

<script>

function b(){

alert("我被click了!!5555~~~~~~~~~~");

}

function a(){

good.onclick=b;//注意onclick不能写成onClick,要不没效果的。

}

</script>

<div id="good" style="height:100px; width:100px;background:#323923;color:white;">点我啊</div>

<input type=button value="添加点击事件" onclick="a();">

<script>

function b(){

alert("我被click了!!5555~~~~~~~~~~");

}

function a(){

good.onclick=b;//注意onclick不能写成onClick,要不没效果的。

}

</script>

<div id="good" style="height:100px; width:100px;background:#323923;color:white;">点我啊</div>

<input type=button value="添加点击事件" onclick="a();">不过在用的时候很是郁闷,如为good添加onMoserOver的事件view plaincopy to clipboardprint?

document.getElementById('QuickUserLoginPart').onmouseover = alert("移过了!");

document.getElementById('QuickUserLoginPart').onmouseover = alert("移过了!");把这个添加在input的onClick里,点击了马上会弹出一个“移过了!”的提示框,然后当你鼠标移过那个ID为GOOD的区域时是什么反应都没有的。一定要把"onmouseover ="后面的内容写上相关定义好的function才行。 光明白这点就浪费了我两个小时时间啊~~~~~~学艺不精啊。

PS:不知道有没有去除页面元素添加事件的JS

再记录个:

JS:动态添加删除元素view plaincopy to clipboardprint?

<HEAD>

<SCRIPT>

function removeElement()

{

try

{

//The first child of the div is the bold element.

var oChild=Div1.children(0);

Div1.removeChild(oChild);

}

catch(x)

{

alert("You have already removed the bold element. Page will be refreshed when you click OK.")

document.location.reload();

}

}

</SCRIPT>

</HEAD>

<BODY>

<DIV ID=Div1 onclick="removeElement()">

Click anywhere in this sentence to remove this <B>Bold1</B><B>Bold2</B> word.

</DIV>

</BODY>

<HEAD>

<SCRIPT>

function removeElement()

{

try

{

//The first child of the div is the bold element.

var oChild=Div1.children(0);

Div1.removeChild(oChild);

}

catch(x)

{

alert("You have already removed the bold element. Page will be refreshed when you click OK.")

document.location.reload();

}

}

</SCRIPT>

</HEAD>

<BODY>

<DIV ID=Div1 onclick="removeElement()">

Click anywhere in this sentence to remove this <B>Bold1</B><B>Bold2</B> word.

</DIV>

</BODY>再记录个不错的站点:

http://www.netvtm.com/

“通过网络汲取营养”——关注国外WEB相关技术,理念,使用技巧...

关于Cookie的使用说明:http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/cookie.asp

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有