首页 \ 问答 \ Javascript事件多次调用(Javascript events multiple calls)

Javascript事件多次调用(Javascript events multiple calls)

我第一次遇到一个非常奇怪的问题。 我正在开发一个包含许多ajax调用的网站(我的ajax调用加载html表,小部件,导航栏等等),在一些ajax响应中我包含加载内容使用的特定javascript(例如fold /展开tbody)。

例如,在我的桌子上,我包括以下代码:

$(document).on("click",".foldandunfold i", function (e) {
    $(this).toggleClass("fa-chevron-right");
    $(this).toggleClass("fa-chevron-down");
    $(this).parents("tbody:first").find("tr:not(:first)").toggle();
}); 

当发送第二个ajax调用时,返回其他内容(使用相同的javascript)我的jquery事件运行两次,如果我加载另一个表,它运行三次等。

将javascript移动到我网站的下半部分解决问题(因为它只加载了一次,但我动态生成了javascript(使用php)特定于widget,我无法将其移出ajax响应。

我可以告诉jquery或JIT javascript编译器清理此事件或类似的事情吗?

对不起我的英文:x


i'm experiencing a very strange issue for the first time. I'm developping a website with many ajax calls (my ajax calls load html tables, widgets, navbar etc..), on some ajax responses i include sepecific javascript used by the loaded content (fold/unfold tbody for exemple).

for example, on my table, i include the following code :

$(document).on("click",".foldandunfold i", function (e) {
    $(this).toggleClass("fa-chevron-right");
    $(this).toggleClass("fa-chevron-down");
    $(this).parents("tbody:first").find("tr:not(:first)").toggle();
}); 

When a second ajax call is send, returning other content (with same javascript) my jquery event runs twice, if i load another table, it runs three times etc..

Moving the javascript to a lower part of my website resolve the issue (because it's loaded only 1 time, but i have dynamicly generated javascript (using php) that is widget specific, i can'nt move it outside the ajax response.

Can i tell jquery or JIT javascript compiler to clean this event or something like that ?

Sorry for my english :x


原文:https://stackoverflow.com/questions/28426320
更新时间:2021-12-26 19:12

最满意答案

在你的头文件中声明:

@property (nonatomic, retain) NSDictionary *originalValues;

然后在.m文件中:

- (void) viewDidLoad {
_originalValues = @{
                    @124: [UIImage imageNamed:@"anyImageName"],
                    @125: [UIImage imageNamed:@"anyImageName"],
                    @126: [UIImage imageNamed:@"anyImageName"],
                    };

}


In your header you have to declare :

@property (nonatomic, retain) NSDictionary *originalValues;

then in .m file do as stated below :

- (void) viewDidLoad {
_originalValues = @{
                    @124: [UIImage imageNamed:@"anyImageName"],
                    @125: [UIImage imageNamed:@"anyImageName"],
                    @126: [UIImage imageNamed:@"anyImageName"],
                    };
}

相关问答

更多