$(this).dialog不是函数

时间:2012-03-27 04:44:35

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

...或者在使用动态HTML时,为什么$(this).dialog()在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函数,其中上面的代码是。我删除它以简化事情。警报A在ALERT B之前发生。警报A表示[object Object]。单击链接时会出现ALERT B,并显示'undefined'

$(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?

2 个答案:

答案 0 :(得分:0)

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

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})
});

答案 1 :(得分:-1)

问题出在Firefox附加组件上。我以安全模式启动Firefox,现在一切正常。我试图找出导致问题的Add-On,​​并且我在各种Add-Ons打开或关闭时重新启动了Firefox,但我现在似乎无法重现该问题。