Javascript webuipopover后未触发主干事件
当我点击“添加”按钮时,警报不会触发。为什么事件没有触发Javascript webuipopover后未触发主干事件,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,当我点击“添加”按钮时,警报不会触发。为什么事件没有触发 var AppView = Backbone.View.extend({ events: { 'click #btnAdd': function() { alert('Add'); } }, initialize: function() { _.bindAll(this, 'render'); this.render();
var AppView = Backbone.View.extend({
events: {
'click #btnAdd': function() {
alert('Add');
}
},
initialize: function() {
_.bindAll(this, 'render');
this.render();
},
render: function() {
var template = _.template($('#myApp').html());
this.$el.html(template);
}
});
var myApp = new AppView({
el: '#container'
});
$('#showPopup').webuiPopover({
width: '500px',
height: 'auto',
padding: true,
trigger: 'click',
closeable: true,
delay: 200,
placement: 'right-bottom',
animation: 'sticky',
dismissable: true,
onShow: function() {
console.log(this);
},
content: '<div>' +
Popop + '<br /><input type="button" value="Add" id="btnAdd"/></div>'
});
var-AppView=Backbone.View.extend({
活动:{
'click#btnAdd':函数(){
警报(“添加”);
}
},
初始化:函数(){
_.bindAll(这是“呈现”);
这个。render();
},
render:function(){
var template=35;.template($('#myApp').html());
这个.$el.html(模板);
}
});
var myApp=new AppView({
el:“#容器”
});
$('#showPopup').webuiPopuver({
宽度:“500px”,
高度:“自动”,
是的,
触发器:“单击”,
可接近的:是的,
延误:200,
位置:'右下',
动画:“粘性”,
可驳回:是的,
onShow:function(){
console.log(this);
},
内容:''+
Popop+“
”
});
快速而准确的答案是将事件附加到正文模板
$('body').on('click', '#btnAdd', function(){
alert('hey buddy');
});
更多信息请访问:
复杂时间:
- 将弹出窗口的模板添加为模板子项
- 将该子项引用到弹出窗口的设置中
$el
根元素中
默认情况下,它插入到文档.body
中
不要为此使用全局jQuery事件侦听器,更喜欢对视图进行范围界定,并尽可能在视图本身中包装插件
此外,请检查如何使用
var-AppView=Backbone.View.extend({
//解析模板一次。
模板:_.template($('#myApp').html()),
活动:{
'单击#btnAdd':'onClick',
},
render:function(){
//这是一个函数,别忘了调用它:“this.template()”
this.el.html(this.template());
//初始化视图渲染内部的插件
此.$('showPopup').webuiPopuver({
container:this.$el,//使用container选项
触发器:“单击”,
可接近的:是的,
位置:'右下',
内容:“测试
”
});
//在渲染函数中返回“this”是一种主干约定。
归还这个;
},
onClick:函数(e){
log(“添加单击事件”);
}
});
//应在视图外部调用“render”。
var myApp=new AppView({
el:“#容器”
}).render()代码>
不会触发
,因为它不在模板中,#btnAdd按钮是由Popover以友好方式创建的,这就是事件未绑定的原因。。.bindAll
如果您使用的是最新版本的主干网,则不需要。
$('body').on('click', '#btnAdd', function(){
alert('hey buddy');
});