gpt4 book ai didi

jQuery - 我的第一个插件,如何防止重复应用到元素

转载 作者:行者123 更新时间:2023-12-01 06:07:02 25 4
gpt4 key购买 nike

我有以下内容:

$('.comment_content').live('focus click', function() {
$(this).bTextAreaResizer();
});

我需要它在点击时实时显示,因为 comment_content 框有时会在页面加载后动态显示。我只是注意到,每次都会继续将 bTextAreaResizer() 重新应用到文本区域,这会使浏览器停止运行。在插件中我有:

(function($){
$.fn.bTextAreaResizer = function(options) {

return this.each(function() {

//bind events
$(this).bind('scroll keyup', function() {
resizeTextArea($(this));
});

我怎么说呢,如果我们还没有绑定(bind)到这个元素呢?这可能吗?

谢谢

最佳答案

我想有更好的方法,但为了避免重复的事件绑定(bind),您可以取消绑定(bind)之前的事件。您可以使用event namespaces为了那个原因。顺便说一句,在这种情况下,您不需要循环所有元素:

return this.unbind('.btar')
.bind('scroll.btar keyup.btar', function() {
resizeTextArea($(this));
});

关于jQuery - 我的第一个插件,如何防止重复应用到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4719865/

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