如何使用id从javascript访问HTML元素,html元素属于另一个html文档

时间:2022-02-17 07:50:32

Continued from the question title:

继续问题标题:

also the other html document is loaded in the parent using AJAX,like this:

还使用AJAX在父级中加载了另一个html文档,如下所示:

$.ajax({
        url: 'calender.aspx',
        cache: false,
        dataType: "html",
        success: function (data) {
           $(".mainBar").html(data);
        }
    });

I need to get a table from calender.aspx which has id 'tableID';

我需要从calender.aspx获取一个表,其中id为'tableID';

3 个解决方案

#1


0  

From within your success callback:

从你成功的回调中:

$(data).find("#tableID");

#2


0  

In your example, you appear to be inserting the document into your document via the line $(".mainBar").html(data);. That being the case, you can then just get it via $("#tableId") once you've done that:

在您的示例中,您似乎是通过$(“。mainBar”)。html(data);行将文档插入到文档中。在这种情况下,您可以在完成后通过$(“#tableId”)获取它:

$(".mainBar").html(data);
var theTable = $("#tableId");

If your goal is not to append everything, but to do something else, you can build up a disconnected DOM tree by doing $(data), and then search it via find:

如果您的目标不是追加所有内容,而是执行其他操作,则可以通过执行$(data)来构建断开连接的DOM树,然后通过find搜索它:

var theTable = $(data).find("#tableId");

#3


0  

As a side note, you can just use .load. However, you would do this:

作为旁注,您可以使用.load。但是,你会这样做:

var $table;

$.ajax({
        url: 'calender.aspx',
        cache: false,
        dataType: "html",
        success: function (data) {
           $table = $(data).find('#tableID');
           $(".mainBar").empty().append($table);
        }
    });

Same thing with .load:

与.load相同的事情:

var $table;
$('.mainBar').load('calendar.aspx #tableID', function(html) {
   $table = $(html).find('#tableID');
});

#1


0  

From within your success callback:

从你成功的回调中:

$(data).find("#tableID");

#2


0  

In your example, you appear to be inserting the document into your document via the line $(".mainBar").html(data);. That being the case, you can then just get it via $("#tableId") once you've done that:

在您的示例中,您似乎是通过$(“。mainBar”)。html(data);行将文档插入到文档中。在这种情况下,您可以在完成后通过$(“#tableId”)获取它:

$(".mainBar").html(data);
var theTable = $("#tableId");

If your goal is not to append everything, but to do something else, you can build up a disconnected DOM tree by doing $(data), and then search it via find:

如果您的目标不是追加所有内容,而是执行其他操作,则可以通过执行$(data)来构建断开连接的DOM树,然后通过find搜索它:

var theTable = $(data).find("#tableId");

#3


0  

As a side note, you can just use .load. However, you would do this:

作为旁注,您可以使用.load。但是,你会这样做:

var $table;

$.ajax({
        url: 'calender.aspx',
        cache: false,
        dataType: "html",
        success: function (data) {
           $table = $(data).find('#tableID');
           $(".mainBar").empty().append($table);
        }
    });

Same thing with .load:

与.load相同的事情:

var $table;
$('.mainBar').load('calendar.aspx #tableID', function(html) {
   $table = $(html).find('#tableID');
});