简体   繁体   中英

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:

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

从您的成功回调中:

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

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

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

As a side note, you can just use .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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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