I want to know if there any possibility to get an element (HTML content) from an external HTML file using the ID selector of this element
我想知道是否有可能使用此元素的ID选择器从外部HTML文件中获取元素(HTML内容)
The external file (template.html
):
外部文件(template.html):
<div id="id1">Content 1 </div>
<div id="id2">Content 2 </div>
Is there any process like this :
有没有这样的过程:
the current file (index.html
)
当前文件(index.html)
<script>
var url = "template.html";
var externalHtmlContent = $ajax(url).document.getElementById("id").innerHTML;
</script>
so externalHtmlContent
would contain "Content 1" value?
那么externalHtmlContent会包含“Content 1”值?
2 个解决方案
#1
3
$.get(url, function (data) {
var html = $(data);
var content_1 = $('#id1', html).text();
// content_1 is "Content 1"
});
#2
2
Try this:
HTML
<div id="#hiddenDiv" style="display: none"></div>
JS
$("#hiddenDiv").load(url,
function() {
var content = $('#id1').html();
//...
}
);
#1
3
$.get(url, function (data) {
var html = $(data);
var content_1 = $('#id1', html).text();
// content_1 is "Content 1"
});
#2
2
Try this:
HTML
<div id="#hiddenDiv" style="display: none"></div>
JS
$("#hiddenDiv").load(url,
function() {
var content = $('#id1').html();
//...
}
);