jQuery / jQuery UI - $(“a”).live(“click”, ...) 不适用于选项卡

发布于 2024-10-15 03:13:15 字数 2100 浏览 8 评论 0 原文

所以我有一些 jQuery UI 选项卡。源代码如下:

HTML:

<div class="tabs">
    <ul>
        <li><a href="#ranges">Ranges</a></li>
        <li><a href="#collections">Collections</a></li>
        <li><a href="#designs">Designs</a></li>
    </ul>
    <div id="ranges"></div>
    <div id="collections"></div>
    <div id="designs"></div>
</div>

jQuery:

$(document).ready(function() {
    $(".tabs").tabs();
});

我的问题是我试图让每个选项卡在单击相关链接时将页面加载到内容面板中。首先,我只是尝试在单击链接时设置所有面板的 html。从下面的代码来看,如果我使用方法 1,它适用于所有链接。但是,如果我使用方法 2,则不会 - 但用于选项卡中的链接(即您单击以选择选项卡的标签)。

方法 1(始终适用于所有链接,但不适用于调用此方法后添加的链接):

$("a").click(function () {
    $("#ranges, #collections, #designs").html("clicked");
});

方法 2(适用于所有未“制表”的链接):

$("a").live("click", function () {
    $("#ranges, #collections, #designs").html("clicked");
});

有谁知道为什么它的行为像这?我真的很想让方法 2 正常工作,因为很可能有一些链接需要我添加点击事件,这些事件是在页面最初加载后添加的。

预先感谢,

理查德

PS 是的,对 .live.click 的函数调用都在 $(document).ready() 函数中,在有人说这可能是问题之前 - 否则它根本无法工作。

编辑:

我想出的解决方案涉及锚点(data-url)中的额外属性和以下代码(输出 404如果无法加载页面,则会出现“未找到”错误)。我的目标是在接下来的几周/几个月内扩展它,使其变得更加强大。

$(".tabs").tabs({
    select: function (event, ui) {
       $(ui.panel).load($(ui.tab).attr("data-url"), function (responseText, textStatus, XMLHttpRequest) {
           switch (XMLHttpRequest.status) {
               case 200: break;
               case 404:
                   $(ui.panel).html("<p>The requested page (" + $(ui.tab).attr("data-url") + ") could not be found.</p>");
                   break;
               default:
                   $(ui.panel).html("<p title='Status: " + XMLHttpRequest.status + "; " + XMLHttpRequest.statusText + "'>An unknown error has occurred.</p>");
                   break;
           };
       });
   }
});

So I have some jQuery UI tabs. The source code is as follows:

HTML:

<div class="tabs">
    <ul>
        <li><a href="#ranges">Ranges</a></li>
        <li><a href="#collections">Collections</a></li>
        <li><a href="#designs">Designs</a></li>
    </ul>
    <div id="ranges"></div>
    <div id="collections"></div>
    <div id="designs"></div>
</div>

jQuery:

$(document).ready(function() {
    $(".tabs").tabs();
});

My problem is that I am trying to make each tab load a page into the content panel on click of the relevant link. To start with I am just trying to set the html of all the panels on clicking a link. From the code below, if I use method 1, it works for all links. However if I use method 2 it doesn't - but only for the links in the tabs (i.e. the labels you click to select a tab).

Method 1 (works for all links all the time, but would not be applied to links which are added after this is called):

$("a").click(function () {
    $("#ranges, #collections, #designs").html("clicked");
});

Method 2 (works for all links which are not "tabified"):

$("a").live("click", function () {
    $("#ranges, #collections, #designs").html("clicked");
});

Does anyone know why it is behaving like this? I would really like to get method 2 working properly as there may well be links which I need to add click events to which are added after the page is originally loaded.

Thanks in advance,

Richard

PS yes the function calls for .live and .click are both in the $(document).ready() function, before anyone says that that may be the problem - otherwise it wouldn't work at all..

Edit:

The solution I came up with involves an extra attribute in the anchors (data-url) and the following code (which outputs a 404 not found error if the page cannot be loaded). I aim to expand this over the next few weeks / months to be a lot more powerful.

$(".tabs").tabs({
    select: function (event, ui) {
       $(ui.panel).load($(ui.tab).attr("data-url"), function (responseText, textStatus, XMLHttpRequest) {
           switch (XMLHttpRequest.status) {
               case 200: break;
               case 404:
                   $(ui.panel).html("<p>The requested page (" + $(ui.tab).attr("data-url") + ") could not be found.</p>");
                   break;
               default:
                   $(ui.panel).html("<p title='Status: " + XMLHttpRequest.status + "; " + XMLHttpRequest.statusText + "'>An unknown error has occurred.</p>");
                   break;
           };
       });
   }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋竹姑娘 2024-10-22 03:13:15

我不知道我是否理解您的目的,但基本上您想在单击选项卡后执行某些操作?
这里用于设置用于选择选项卡的回调函数的文档。

编辑:不知道该链接是否正常工作,您想查看“事件”下的“选择”。但基本上它是:

$("#tabs").tabs({
    select: function(event, ui) { ... }
});

ui 包含有关单击的选项卡的信息。

I don't know if I understand what you are going for but basically you want to do something once a tab is clicked?
Here's the docs for setting up a callback function for selecting a tab.

EDIT: Don't know if that link is working correctly, you want to look at select under Events. But basically it is:

$("#tabs").tabs({
    select: function(event, ui) { ... }
});

Where ui has information on the tab that was clicked.

分开我的手 2024-10-22 03:13:15

jQuery UI 选项卡有一个突出的问题,即使用 return false; 而不是 event.preventDefault();。这样就有效的防止了live所依赖的事件冒泡。计划通过 jQuery UI 1.9 修复此问题,但与此同时,最好的方法是使用内置的在 @rolfwaffle 建议的选择事件中。

jQuery UI tabs has an outstanding issue where return false; is used instead of event.preventDefault();. This effectively prevents event bubbling which live depends on. This is scheduled to be fixed with jQuery UI 1.9 but in the meantime the best approach is use the built in select event as suggested by @rolfwaffle.

冷…雨湿花 2024-10-22 03:13:15

也许您正在使用的 tabs() 插件正在调用 event.preventDefault(); (参考) 一旦它创建了它的选项卡。

然后它捕获单击事件并且冒泡停止,因此它不会调用您的单击函数。在 jQuery 中,这是通过

$(element).click(function(){
    // Do stuff, then
    return false; // Cancels the event
});

您必须更改 tabs() 插件代码并删除此 return false; 来完成的。声明,或者如果幸运的话,该插件可能有一个选项来禁用该行为。

编辑:现在我看到你正在使用 jQuery UI。然后你应该检查那里的文档,因为它是一个很棒的插件,如果你正确处理 html 并传递正确的选项,它会做你想做的任何事情。

Maybe the tabs() plugin that you are using is calling event.preventDefault(); (Reference) once it has created it's tabs.

Then it captures the click event and the bubbling stops, so it doesn't invoke your click-function. In jQuery this is done with

$(element).click(function(){
    // Do stuff, then
    return false; // Cancels the event
});

You'd have to alter the tabs() plugin code and remove this return false; statement, OR if you are lucky, the plugin might have an option to disable that behavior.

EDIT: Now I see you're using jQuery UI. Then you should check the documentation there, since it is an awesome plugin, it will do anything you want if you do the html right and pass it the right options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文