【问题标题】:$(this).dialog is not a function$(this).dialog 不是函数
【发布时间】:2012-04-10 15:03:06
【问题描述】:

...或者为什么 $(this).dialog() 在使用动态 HTML 时在 Firefox 中会失败?

我有一个点击事件,它在网页上打开一个 jQuery 模式对话框,它在 Chrome 和 IE 中运行良好,但在 Firefox 中却不行。

下面是相关代码:

var dialogId = 'uniqueName-' + Math.floor(Math.random() * 1000)
var dialogDiv = $(document.createElement('div')).attr("id", dialogId);

dialogDiv.load(this.href, function () {
    var dialog = $(this).dialog({ autoOpen: false });
    ...
});

在 Firefox 11 中,$(this).dialog({ autoOpen: false }) 失败并显示以下错误消息:

$(this).dialog 不是函数

但在 IE 9 和 Chrome 17 中一切正常。有什么线索吗?

更新:

这是上面代码所在的document.ready 函数。我删除了它以简化事情。 ALERT A 发生在 ALERT B 之前。ALERT A 显示[object Object]。当我点击链接并显示'undefined' 时,会出现警报 B。

$(function () {

    alert($.ui);    // ALERT A

    // Wire up the click event of any dialog links
    $('.dialogLink').live('click', function () {
        alert($.ui);    // ALERT B
        return false;
    });
});

更新 2:

既然我指出了问题的出处,我重新表述了我的问题,并在此处发布了重现原始问题的最少代码:Why is FF on OS X losing jQuery-UI in click event handler?

【问题讨论】:

  • 你能用console.log或者一个简单的alert来检查jquery是否工作正常吗?此外,如果它的 jquery UI,使用 firebug 检查它是否在 ff 中加载正常!
  • @MarshallMathews:jQuery 工作正常,因为使用 $(document.createElement('div')).attr("id", dialogId) 初始化了 dialogDiv。我用萤火虫检查了这个。您将如何使用 Firebug 检查 jQuery-UI 是否可以正常加载?
  • console.log("This explains the dialog method: " + $.ui.dialog) 如果为 false,则可能 UI 未正确加载
  • 如果您使用的是 jQuery,为什么要使用 $(document.createElement('div')).attr("id", dialogId); 创建一个 div?为什么不直接使用$('<div></div>').attr("id", dialogId);?
  • 从“/echo/html”加载时在 FF11 中工作正常。一定是代码中发生了其他事情。

标签: javascript jquery jquery-ui firefox jquery-ui-dialog


【解决方案1】:

问题出在 Firefox 附加组件上。我在安全模式下启动了 Firefox,现在一切正常。我试图确定是哪个加载项导致了问题,并在打开或关闭各种加载项的情况下重新启动了 Firefox,但我现在似乎无法重现该问题。

【讨论】:

    【解决方案2】:

    如果我没记错的话,你会遇到一些语法/链接问题:

    var dialogId = 'uniqueName-' + Math.floor(Math.random() * 1000)
    //var dialogDiv = $(document.createElement('div')).attr("id", dialogId);
    //dialogDiv equals the attribute 'id'
    //try and console.log(dialogDiv) right here. what I think you want is:
    
    var dialogDiv = $("<div />");
    dialogDiv.attr("id", dialogId).load(this.href, function () {
        var dialog = $(this).dialog({ autoOpen: false });
        ...
    });
    

    我也不认为这是初始化您正在尝试做的事情的正确方法...您能在您的页面上描述发生了什么吗?

    你可能会考虑做这样的事情:

    var dialogId = 'uniqueName-' + Math.floor(Math.random() * 1000);
    //Build some HTML here in the dialog div, or in a string.
    theHTML = $('#'+dialogId).html() || "<p>This is a string of HTML</p>";
    $('body').on('click', ".button" function () {
        console.log($.ui);
        $.dialog({autoOpen:true, html: theHTML})
    });
    

    【讨论】:

    • 问题是我在使用 Firefox 时在单击事件处理程序中丢失了 jQuery-UI。我的代码在 Google Chrome 和 IE 上运行良好。我认为您提出的更改不会解决我要解决的问题。
    • 尝试将 $.ui 放在那块中,我没有 ff11 但它在 jsfiddle.net 中对我有用
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 2018-01-19
    相关资源
    最近更新 更多