this.remove()不是函数.怎么会?
var vehicle = function () {
return {
init: function () {
jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) {
e.preventDefault();
this.remove();
});
},
remove: function () {
alert('test');
}
}
}();
jQuery().ready(vehicle.init);
Run Code Online (Sandbox Code Playgroud)
对困惑感到抱歉.我正在尝试调用自己的"删除"功能.这只是一个管理我页面上的车辆的类.这是它的开始,它将具有比init/remove更多的功能.
this是一个DOM元素.要使用jQuery的.remove()方法,需要将其包装在jQuery对象中.
$(this).remove();
Run Code Online (Sandbox Code Playgroud)
编辑:如果您希望remove()在vehicle对象中调用该函数,则调用:
vehicle.remove();
Run Code Online (Sandbox Code Playgroud)
此外,如果您希望缩短.ready()通话时间,可以执行以下操作:
jQuery(vehicle.init);
Run Code Online (Sandbox Code Playgroud)
该
jQuery().ready()技术仍然适用于1.4,但已被弃用.请使用jQuery(document).ready()或jQuery(function(){}).
既然您说您正在尝试调用自己的remove函数,那么具体方法如下:
var vehicle = (function () {
return {
init: function () {
var that = this; // step one
jQuery('.vehicle-year-profile .options .delete').bind('click', function (e) {
e.preventDefault();
that.remove();
});
},
remove: function () {
alert('test');
}
}
}()); // step zero - wrap the immediate invocation in parens
jQuery(function () {
vehicle.init(); // step two
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5847 次 |
| 最近记录: |