作为JS和jquery的新角色,我似乎无法让这件事情起作用!该警报提供undefined
!通过jquery .on()函数将单击元素的属性值传递给外部函数
this.$el.on('click', '.chorse-item',{id:$(this).attr('id')}, this.handleItem);
this.handleItem = function(event) {
alert(event.data.id);
}
它显示了 “ID” 完全符合
this.$el.on('click', '.chorse-item', function(){alert($(this).attr('id'))});
我怀疑$(this).attr('id')
被掉下来的范围或东西吗?原因
this.$el.on('click', '.chorse-item',{id:"Testing"}, this.handleItem);
显示“测试”。
显示完整的代码?我不明白'this'是什么? – chris97ong
您可以将ID存储在第一行上方的变量中。 var id = $(this).attr('id')。那么你的第三个参数就是{id:id} – walkerrandophsmith
那么,如果使用第二种方法会出现什么问题? –