index.js$(function(){
$("#btn").on("click",function(){
$.ajax({ url: 'page.html', type: 'post', success: function (responseText) {
$('#div1').html(responseText);
}
});
});
$("#test").on("click",function(){
alert('hello world!');
});
});demo.html<script type="text/javascript" src="index.js"></script><input type="button" id="btn" value="加載" /><div id="div1"></div>page.html<input type="button" id="test" value="執行" />請問:當demo頁面加載page頁面后,html代碼就會被加載到div1容器里面,之間加載的index.js對于新內容不起作用,點擊執行按鈕是無效的。請問如何讓加載的頁面中javascript代碼能夠執行?實踐場景:頁面依賴第三方表單驗證(通過class屬性定義驗證規則),動態新添加了表單元素,可是新表單元素的驗證規則不起作用。
jquery ajax加載頁面中的js代碼如何執行?
守候你守候我
2018-08-13 13:10:19