gpt4 book ai didi

javascript - 替换 View 后主干 View 事件未触发,然后放回页面上

转载 作者:行者123 更新时间:2023-11-30 18:22:50 24 4
gpt4 key购买 nike

问题:

在使用 $element.html(anotherView) 将 View 替换为其他内容并使用 #element 将其放回页面后, View 内设置的事件不会触发。 html(theView).

示例:

var BuggyView = Backbone.View.extend({
// Event works the at first, but not after the view is replaced, then added back
// onto the page
events:{
'click #some-element': 'on_click_some_element'
},
// This function gets called before the view is replaced by another view, but
// not after it is added back into the page the second time
on_click_some_element:function(event){
alert('User clicked "some element"');
}
});

此代码执行后事件起作用:

// Create the new view:
var buggyView = new BuggyView({});
// Add the view to the page
$element.html(buggyView.el);

此代码稍后会在页面上的 View 被其他内容替换时发生:

$element.html(anotherView.el);

将 View 添加回页面后,事件不再起作用:

$element.html(buggyView.el);

最佳答案

很可能当您删除 View 时,您也删除了 View 的绑定(bind)事件,您可能需要 redelgate your events .或者不使用 (jquery) 。 remove使用 .detach .

关于javascript - 替换 View 后主干 View 事件未触发,然后放回页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11638413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com