简体   繁体   English

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

[英]how do I access a HTML element from javascript using id,the html element belongs to another html document

Continued from the question title: 接续问题标题:

also the other html document is loaded in the parent using AJAX,like this: 另一个HTML文档也使用AJAX加载到父级中,如下所示:

$.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';

从您的成功回调中:

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

In your example, you appear to be inserting the document into your document via the line $(".mainBar").html(data); 在您的示例中,您似乎是通过$(".mainBar").html(data);文档插入文档中的$(".mainBar").html(data); . That being the case, you can then just get it via $("#tableId") once you've done that: 在这种情况下,一旦完成,您就可以通过$("#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");

As a side note, you can just use .load . 作为附带说明,您可以只使用.load However, you would do this: 但是,您可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM