var iGotYou = new Promise(function (res, rej) {
$.ajax({
//ajax paramaters
})
.done(function( data ) {
//handle the data as necessary...
//then resolve the Promise
res();
});
});
//the Promise has been resolved
iGotYou.then(function (response) {
//add the event listener now that the promise has been fulfilled
document.getElementById('someId').addEventListener('click', function (e) {
//whatever you want to do on click event
});
})
5条答案
按热度按时间yyyllmsg1#
两种方式:
1)使用
.on()
绑定到非动态父容器字符串
2)在一次调用完成后绑定新元素
型
前者通常会导致更快的点击响应时间,但可能也会减慢点击响应速度。
liwlm1x92#
使用jQuery的. on()来处理事件委托。您提供的第一个元素是静态元素(永远不会删除/替换)。第一个参数是您希望委托的事件,鼠标悬停/单击等。第二个参数是我们希望在事件发生时触发事件的元素。第三个参数是回调,这是事件触发时运行的函数。
字符串
w46czmvw3#
字符串
在
.parent-div
中插入的所有新元素都将拥有监听器onclick
7ivaypg94#
再加上Populus的答案,这是伟大的,因为它是,一个逻辑上等效的解决方案,他的第二个选择将使用Promises:
字符串
nzk0hqpo5#
我不完全确定你在这里问什么,但是你可以使用jQuery的.on()函数绑定到文档中已经存在的元素,或者将来会存在的元素。
这里有一个简单的例子:
字符串