使用纯JavaScript将事件处理程序附加到具有类的动态创建的元素
我构建了一个将事件处理程序附加到类的函数。它可以很好地处理在页面加载期间已经存在于DOM中的元素,但是在页面加载之后动态添加到DOM中的元素会以静默方式失败。我感谢你的建议。下面的代码是我正在开发的名为launchjs的JavaScript库的一部分。其他建议的解决方案只指向我已经知道如何使用的JQuery代码,我需要一个没有JQuery的JavaScript解决方案 我相信有一个解决方案接近Ram swaroop提供的我所需要的解决方案,但我不认为它指向了我在将事件附加到动态创建的元素时遇到的根本问题,并且它没有像本页上公认的答案那样对其进行透彻的解释 代码的最终结果应该允许用户使用以下代码结构将事件附加到具有类的元素使用纯JavaScript将事件处理程序附加到具有类的动态创建的元素,javascript,Javascript,我构建了一个将事件处理程序附加到类的函数。它可以很好地处理在页面加载期间已经存在于DOM中的元素,但是在页面加载之后动态添加到DOM中的元素会以静默方式失败。我感谢你的建议。下面的代码是我正在开发的名为launchjs的JavaScript库的一部分。其他建议的解决方案只指向我已经知道如何使用的JQuery代码,我需要一个没有JQuery的JavaScript解决方案 我相信有一个解决方案接近Ram swaroop提供的我所需要的解决方案,但我不认为它指向了我在将事件附加到动态创建的元素时遇到的
l(".className").on("click",function(event){
//do something here
});
JavaScript:
/*
The type parameter passed in to this function is "click" and the
selector variable value equals .view
Note: the isClass function returns true if the selector value is prefixed
with a .
*/
this.on = function(type,fn){//begin on function
//store the events
var events = ["click","mouseover","mouseout","submit","dblclick"];
//if the event array contains the event and the selector is not a class
if(events.indexOf(type) !== -1 && !isClass()){//begin if then
//add the event listenter to the document
document.addEventListener(type,function(event){//begin function
/*
if the event listener target id equals the selector id passed to the launch
function and the selectors doesn't contain the . prefix or class prefix.
*/
if(event.target.id === selector){//begin if then
//call the function passed in as a parameter, pass the event so it can be used.
fn(event);
}//end if then
});//end function
}//end if then
//if the event array contains the event and the selector is a class
if(events.indexOf(type) !== -1 && isClass()){//begin if then
//store the collection of classes
var classes = document.getElementsByClassName(selector.split(".")[1]);
//loop through the classes and add the event listeners
for(var i = 0; i < classes.length; i++){
classes[i].addEventListener(type,function(event){//begin addEventListener function
//add functionality for classes
if(event.target.className === selector.split(".")[1] && isClass()){//begin if then
//call the function passed in as a parameter,pass the event so it can be used.
fn(event);
}//end if then
});//end addEventListener function
}//end for loop
}//end if then
};//end on function
/*
传递给此函数的类型参数是“click”,并且
选择器变量值等于。视图
注意:如果选择器值带有前缀,isClass函数将返回true
用一个。
*/
this.on=函数(type,fn){//begin on函数
//存储事件
var events=[“单击”、“鼠标悬停”、“鼠标悬停”、“提交”、“dblclick”];
//如果事件数组包含事件且选择器不是类
if(events.indexOf(type)!=-1&&!isClass()){//beginif then
//将事件列表项添加到文档中
addEventListener(类型、函数(事件){//begin函数
/*
如果事件侦听器目标id等于传递给启动的选择器id
函数和选择器不包含.prefix或class前缀。
*/
if(event.target.id==选择器){//如果开始,则
//调用作为参数传入的函数,传递事件以便可以使用。
fn(事件);
}//如果那样就结束
});//结束函数
}//如果那样就结束
//如果事件数组包含事件且选择器为类
if(events.indexOf(type)!=-1&&isClass()){//beginif then
//存储类的集合
var classes=document.getElementsByClassName(selector.split(“.”[1]);
//循环遍历类并添加事件侦听器
对于(var i=0;i
您需要在每次添加新元素时运行此代码,同时确保没有将事件侦听器两次附加到旧元素,或者可以使用委派事件的技术,如从jQuery中
简而言之,将事件侦听器附加到全局容器,并检查指定类的单击元素。以下是一篇关于这一问题的文章,其概念如下:
还有一个图书馆,由同一个人为这个特定案例编写:
请参见我的简短概念示例:
var-area=document.getElementsByClassName('clickable-area')[0];
area.onclick=函数(e){
如果(e.target.className==='addchildren'){
var button=document.createElement('button');
button.className='子按钮';
button.innerText='子按钮';
区域。追加子对象(按钮);
}else if(e.target.className==‘子按钮’){
e、 target.innerText='单击动态添加的子项';
}
}
html,
身体{
宽度:100%;
身高:100%;
填充:0;
保证金:0;
}
.可点击区域{
显示:块;
光标:指针;
背景:#eee;
宽度:100%;
身高:100%;
}
添加子项
您需要在每次添加新元素时运行此代码,同时确保没有将事件侦听器两次附加到旧元素,或者可以使用委派事件的技术,如从jQuery中
简而言之,将事件侦听器附加到全局容器,并检查指定类的单击元素。以下是一篇关于这一问题的文章,其概念如下:
还有一个图书馆,由同一个人为这个特定案例编写:
请参见我的简短概念示例:
var-area=document.getElementsByClassName('clickable-area')[0];
area.onclick=函数(e){
如果(e.target.className==='addchildren'){
var button=document.createElement('button');
button.className='子按钮';
button.innerText='子按钮';
区域。追加子对象(按钮);
}else if(e.target.className==‘子按钮’){
e、 target.innerText='单击动态添加的子项';
}
}
html,
身体{
宽度:100%;
身高:100%;
填充:0;
保证金:0;
}
.可点击区域{
显示:块;
光标:指针;
背景:#eee;
宽度:100%;
身高:100%;
}
添加子项
此代码是否在添加元素后运行,或在DOM就绪时仅运行一次?在DOM就绪时仅运行一次。请参阅:此代码是否在添加元素后运行,或在DOM就绪时仅运行一次?在DOM就绪时仅运行一次。请参阅:谢谢,你的回答是一个很好的解释,我能够满足我的功能的原则。@LarryLane很乐意帮忙!谢谢你,你的回答是一个很好的解释,我能够满足我的on功能的原则。@LarryLane很乐意帮忙!