代碼是這樣的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");}現在使用hash + ajax的方式做單頁,我進入頁面的地址為localhost:7777/ 之后我進行了 location.hash ='#main'操作,這時候觸發了onhashchange函數之后發送了請求插入了片段到一個div中。首頁的js能夠正常運行(就是main.html片段中的js能夠正常執行)。但是當我點擊導航欄的其它頁面再回到localhost:7777/#/main 的時候插入的html中的js不再執行?請問下這是為什么?困擾了我幾天的問題希望有大大幫助看看。說明:頁面中的js都是通過webpack中的HtmlwebpackPlugin插件插入的
ajax加載的html內容中包含js的問題
暮色呼如
2018-10-18 11:10:44