最近做一個登錄仿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的區(qū)域時是什么反應都沒有的。一定要把"onmouseover ="后面的內容寫上相關定義好的function才行。 光明白這點就浪費了我兩個小時時間啊~~~~~~學藝不精啊。
PS:不知道有沒有去除頁面元素添加事件的JS
再記錄個:
JS:動態(tài)添加刪除元素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>再記錄個不錯的站點:
“通過網絡汲取營養(yǎng)”——關注國外WEB相關技術,理念,使用技巧...
關于Cookie的使用說明: