8

我想<div>在 jQuery UI 模态对话框结束时刷新一个。

我的代码是:

var dialog = jQuery('#divPopup').dialog({
    autoOpen: false,
    height: 450,
    width: 650,
    modal: true,
    open: function(event, ui) {
        jQuery('body').css('overflow', 'hidden');
    },
    close: function(event, ui) {
        jQuery('#divPopup').dialog('destroy').remove();
        jQuery("#bodyId").load("http://www.xyz.com/ #bodyId");
    }
});

但不是替换它,而是<div>在旧的内部添加新的<div>

<div id="bodyId">
    <div id="bodyId">
        New Response
    </div>
</div>

我想bodyId用新的 div 替换旧的 div bodyId

4

3 回答 3

12

Try to replace this:

jQuery("#bodyId").load("http://www.xyz.com/ #bodyId");

... with this:

jQuery("#bodyId").load("http://www.xyz.com/ #bodyId > *");

This works because you can use any selector after the URL. By using #bodyId > * instead of just #bodyId, you match everything that is inside the div, instead of the div itself.

You need this because .load() will not replace an element; it will append the result of the AJAX call to the element.

Alternatively, you could use .get() to load the data and manually perform the selection, like so:

$.get('http://www.xyz.com/', function(data) {
    var newContent = $(data).find('#bodyId').children();
    $('#bodyId').empty().append(newContent);
});

Examples of both methods are online here: http://jsfiddle.net/PPvG/47qz3/

于 2012-01-16T06:03:11.827 回答
0

您也可以使用 jquery.get。

$.get('http://www.xyz.com/', function(data) {
  $('#bodyId').html(data);
});
于 2012-01-16T06:10:26.820 回答
-4

您可能会像这样使用此代码

$('#bodyID').remove();
$('#bodyID').load();
于 2012-01-16T06:00:06.873 回答