gpt4 book ai didi

javascript - 变量范围 : this. remove 不是函数

转载 作者:数据小太阳 更新时间:2023-10-29 05:59:25 24 4
gpt4 key购买 nike

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);

抱歉造成混淆。我正在尝试调用我自己的“删除”功能。这只是一个在我的页面上管理车辆的类。这是它的开始,它将具有比 init/remove 更多的功能。

最佳答案

this 是一个 DOM 元素。要使用 jQuery 的 .remove() 方法,您需要将其包装在一个 jQuery 对象中。

$(this).remove();

编辑:如果您希望调用vehicle 对象中的remove() 函数,则调用:

vehicle.remove();

此外,如果您希望缩短 .ready() 调用,您可以这样做:

jQuery(vehicle.init);

来自jQuery 1.4 release notes :

The jQuery().ready() technique still works in 1.4 but it has been deprecated. Please use either jQuery(document).ready() or jQuery(function(){}).

关于javascript - 变量范围 : this. remove 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3754273/

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