带有$(document).ready等效的jQuery UI选项卡

时间:2021-11-04 09:23:18

I'm trying to use jQuery UI tabs much like a Master Page in ASP.Net. I have specific javascript that needs to run once each tab is loaded. I've tried to have a $(document).ready function in each page, but I've read that's bad practice and only seems to work in IE.

我正在尝试使用jQuery UI选项卡,就像ASP.Net中的母版页一样。每个选项卡加载后,我都需要运行特定的javascript。我试图在每个页面中都有一个$(document).ready函数,但我读过这个不好的做法,似乎只能在IE中运行。

I know there's a load event, but I need to the event to be specific for each tab and I'm not sure how to accomplish this, or if it's even possible. I may have to revert to using and actual MasterPage with postbacks for each page load.

我知道有一个加载事件,但我需要将事件特定于每个选项卡,我不知道如何实现这一点,或者甚至可能。我可能不得不恢复使用和实际的MasterPage与每个页面加载的回发。

3 个解决方案

#1


Where did you read that it's bad practice?

你在哪里读到这是不好的做法?

Afaik, having multiple $(document).ready()'s will simply make them "merge", so that all code that is in either of them will run. I don't remember if that's new in jQuery 1.3 though, so you might want to check that out.

Afaik,有多个$(document).ready()只会使它们“合并”,因此其中任何一个代码都会运行。我不记得这是否是jQuery 1.3中的新功能,所以你可能想要检查一下。

#2


$(document).ready() is not bad practice, and it works in every browser that jQuery works in. If your events are only firing in IE, you've got a bug somewhere in your own code.

$(document).ready()是不错的做法,它适用于jQuery工作的每个浏览器。如果你的事件只在IE中触发,你在自己的代码中有一个错误。

I have dozens of ready functions in my applications and no problems.

我的应用程序中有很多现成功能,没有问题。

#3


It is not bad practice to have multiple $().ready's, unless you've needlessly broken it up into multiple ones. In the case of a tab being loaded in ajaxily, IMHO putting a $(document).ready inside the loaded-in content is probably the best way to go about having some tab-specific actions happen. I use that for re-binding the newly loaded elements as they are injected into the freshly loaded tab.

拥有多个$()。ready,这是不错的做法,除非你不必要地把它分成多个。如果选项卡是在ajaxily中加载的,恕我直言将$(document).ready放入加载的内容中可能是实现某些特定于选项卡的操作的最佳方式。我使用它来重新绑定新加载的元素,因为它们被注入到新加载的选项卡中。

#1


Where did you read that it's bad practice?

你在哪里读到这是不好的做法?

Afaik, having multiple $(document).ready()'s will simply make them "merge", so that all code that is in either of them will run. I don't remember if that's new in jQuery 1.3 though, so you might want to check that out.

Afaik,有多个$(document).ready()只会使它们“合并”,因此其中任何一个代码都会运行。我不记得这是否是jQuery 1.3中的新功能,所以你可能想要检查一下。

#2


$(document).ready() is not bad practice, and it works in every browser that jQuery works in. If your events are only firing in IE, you've got a bug somewhere in your own code.

$(document).ready()是不错的做法,它适用于jQuery工作的每个浏览器。如果你的事件只在IE中触发,你在自己的代码中有一个错误。

I have dozens of ready functions in my applications and no problems.

我的应用程序中有很多现成功能,没有问题。

#3


It is not bad practice to have multiple $().ready's, unless you've needlessly broken it up into multiple ones. In the case of a tab being loaded in ajaxily, IMHO putting a $(document).ready inside the loaded-in content is probably the best way to go about having some tab-specific actions happen. I use that for re-binding the newly loaded elements as they are injected into the freshly loaded tab.

拥有多个$()。ready,这是不错的做法,除非你不必要地把它分成多个。如果选项卡是在ajaxily中加载的,恕我直言将$(document).ready放入加载的内容中可能是实现某些特定于选项卡的操作的最佳方式。我使用它来重新绑定新加载的元素,因为它们被注入到新加载的选项卡中。