3 回答

TA貢獻1803條經驗 獲得超3個贊
您不能以這種方式將HTML嵌入Javascript?;旧?,您想要的是嵌入一個腳本元素,單擊按鈕時指向某個javascript文件。這可以通過將事件與DOM結合來完成:
<script type="application/javascript">
function loadJS(file) {
// DOM: Create the script element
var jsElm = document.createElement("script");
// set the type attribute
jsElm.type = "application/javascript";
// make the script element load file
jsElm.src = file;
// finally insert the element to the body element in order to load the script
document.body.appendChild(jsElm);
}
</script>
<button onclick="loadJS('file1.js');">Load file1.js</button>
<button onclick="loadJS('file2.js');">Load file2.js</button>
添加回答
舉報