2012-03-27 54 views
0

...或者为什么$(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 })失败,出现以下错误消息:

$(本).dialog不是函数

但在IE 9中,Chrome 17的一切工作正常。任何线索为什么是这样?

UPDATE:

这里是我的document.ready函数,其中上面的代码了。我删除它来简化事情。 ALERT A在ALERT B之前发生。ALERT 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?

+0

你可以使用console.log或简单的警报来检查jquery是否工作正常吗?此外,如果它的jQuery UI,使用萤火虫来检查它是否加载ff罚款! – 2012-03-27 04:53:11

+0

@MarshallMathews:jQuery工作正常,因为dialogDiv用$(document.createElement('div'))。attr(“id”,dialogId)正确初始化。我用萤火虫检查了这个。你将如何去检查jQuery-UI是否可以用Firebug加载? – 2012-03-27 17:21:20

+0

'console.log(“这解释了对话方法:”+ $ .ui.dialog)'如果为false,那么UI可能没有加载正确 – 2012-03-27 18:16:10

回答

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

问题是,我在使用Firefox时在点击事件处理程序中丢失了jQuery-UI。我的代码在Google Chrome和IE中运行良好。我不认为你提出的改变将解决我正在努力解决的问题。 – 2012-03-28 01:52:31

+0

尝试把$ .ui放在那一块,我没有ff11,但它适用于我在jsfiddle.net – Relic 2012-03-28 15:21:28

-1

的问题是用火狐浏览器插件。我以安全模式启动Firefox,现在一切正常。我试图确定哪个附加组件导致了问题,并且我重新启动了Firefox,并打开或关闭了各种附加组件,但我现在似乎无法重现此问题。