動(dòng)態(tài)添加js事件實(shí)現(xiàn)代碼
更新時(shí)間:2009年03月12日 23:46:14 作者:
動(dòng)態(tài)添加js事件,主要是不用具體指定位置的事件,這種動(dòng)態(tài)添加事件的方法比較方便,并可以擴(kuò)展等。
//表單對(duì)象
function GetObject(Name)
{
var inputlist=document.all.tags('input');
var i=0;
for(i=0;i<inputlist.length;i++)
{
var input=inputlist[i];
if(input.id.indexOf(Name) !=-1)
{
return input;
}
}
return null;
}
//設(shè)置對(duì)象事件
function setEvent(Object,EventName,EventFunction)
{
if(Object ==null)
return;
if(window.addEventListener)
{
//其它瀏覽器的事件代碼: Mozilla, Netscape, Firefox
//添加的事件的順序即執(zhí)行順序 //注意用 addEventListener 添加帶on的事件,不用加on
Object.addEventListener(EventName.replace('on',''), EventFunction, false);
}
else
{
//IE 的事件代碼 在原先事件上添加 add 方法
Object.attachEvent(EventName,EventFunction);
}
}
//失去焦點(diǎn)函數(shù)
function blur()
{
var size=lSize.value * 0.3;
pSize.value=ForDight(size,0);
}
//四舍五入函數(shù)
function ForDight(Dight,How)
{
var Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How);
return Dight;
}
//執(zhí)行
var lSize=GetObject('STNumberTextBox1');
var pSize=GetObject('STNumberTextBox2');
setEvent(lSize,'onblur',blur);
function GetObject(Name)
{
var inputlist=document.all.tags('input');
var i=0;
for(i=0;i<inputlist.length;i++)
{
var input=inputlist[i];
if(input.id.indexOf(Name) !=-1)
{
return input;
}
}
return null;
}
//設(shè)置對(duì)象事件
function setEvent(Object,EventName,EventFunction)
{
if(Object ==null)
return;
if(window.addEventListener)
{
//其它瀏覽器的事件代碼: Mozilla, Netscape, Firefox
//添加的事件的順序即執(zhí)行順序 //注意用 addEventListener 添加帶on的事件,不用加on
Object.addEventListener(EventName.replace('on',''), EventFunction, false);
}
else
{
//IE 的事件代碼 在原先事件上添加 add 方法
Object.attachEvent(EventName,EventFunction);
}
}
//失去焦點(diǎn)函數(shù)
function blur()
{
var size=lSize.value * 0.3;
pSize.value=ForDight(size,0);
}
//四舍五入函數(shù)
function ForDight(Dight,How)
{
var Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How);
return Dight;
}
//執(zhí)行
var lSize=GetObject('STNumberTextBox1');
var pSize=GetObject('STNumberTextBox2');
setEvent(lSize,'onblur',blur);
相關(guān)文章
JS 設(shè)計(jì)模式之:工廠模式定義與實(shí)現(xiàn)方法淺析
這篇文章主要介紹了JS 設(shè)計(jì)模式之:工廠模式,結(jié)合實(shí)例形式分析了JS 工廠模式基本概念、原理、定義、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05uniapp頁面間傳參的幾種方法實(shí)例總結(jié)
在進(jìn)行頁面的跳轉(zhuǎn)的時(shí)候,往往需要我們將一些參數(shù)攜帶著傳遞過去,下面這篇文章主要給大家介紹了關(guān)于uniapp頁面間傳參的幾種方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12JavaScript自定義等待wait函數(shù)實(shí)例分析
這篇文章主要介紹了JavaScript自定義等待wait函數(shù),實(shí)例分析了自定義等待函數(shù)的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2015-03-03js類定義函數(shù)時(shí)用prototype與不用的區(qū)別示例介紹
沒有使用prototype的方法相當(dāng)于類的靜態(tài)方法,相反,使用prototype的方法相當(dāng)于類的實(shí)例方法,不許new后才能使用2014-06-06