![](/img/trans.png)
[英]Replacing a div with the content of another html-file ('common/…html) with VanillaJS instead of jQuery
[英]store the content of an html-file into an jquery array
大家好,
我嘗試在jquery的幫助下將HTML文件的內容存儲到數組元素中。
如果我嘗試:
$(document).ready(
function(){
html = $.parseHTML( '<p>say hello</p><p>or not</p><h3>just naother TAG</h3>' );
alert(html[0].innerHTML);
alert(html[1].innerHTML);
alert(html[2].innerHTML);
});
一切順利。
但是如果我將parseHTML的Brackets中的String替換為如下所示的html文件
$(document).ready(
function(){
html = $.parseHTML( 'navi.html' );
alert(html[0].innerHTML);
alert(html[1].innerHTML);
alert(html[2].innerHTML);
});
警報框告訴我“未定義”。
navi.html文件的內容是
<p>say hello</p><p>or not</p><h3>just another TAG</h3>
請你幫助我好嗎?
我已經閱讀過類似的示例,但無法達到目標。
謝謝,Thorsten
jQuery的$.parseHTML
不會獲取文件,而是從字符串中解析HTML,僅此而已。
如果要獲取文件,則必須使用ajax,例如$.get
$.get('navi.html', function(data) {
console.log( data );
}, 'html');
在最后添加dataType $.parseHTML
自動調用$.parseHTML
,並解析返回的數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.