代碼是這樣的export function init(){window.onhashchange = function(){ var hash = location.hash; if(location.href.indexOf('?') !== -1){
hash = location.href.substring(location.href.indexOf('#')+1,location.href.indexOf('?'))
}else{
hash = hash.substring(1,hash.length);
}
loadHtmlData(hash);
}}function loadHtmlData(page){ $.get(page + '.html',function(data){
$('#ajaxContainer').html(data);
},"html");}現(xiàn)在使用hash + ajax的方式做單頁,我進(jìn)入頁面的地址為localhost:7777/ 之后我進(jìn)行了 location.hash ='#main'操作,這時(shí)候觸發(fā)了onhashchange函數(shù)之后發(fā)送了請求插入了片段到一個(gè)div中。首頁的js能夠正常運(yùn)行(就是main.html片段中的js能夠正常執(zhí)行)。但是當(dāng)我點(diǎn)擊導(dǎo)航欄的其它頁面再回到localhost:7777/#/main 的時(shí)候插入的html中的js不再執(zhí)行?請問下這是為什么?困擾了我?guī)滋斓膯栴}希望有大大幫助看看。說明:頁面中的js都是通過webpack中的HtmlwebpackPlugin插件插入的
ajax加載的html內(nèi)容中包含js的問題
暮色呼如
2018-10-18 11:10:44