我用按鈕和功能將 b.js 檔案加載到 HTML 中。當用戶鍵入“是”并單擊“確定”按鈕時,“b.js”必須加載到頁面中(不在新視窗或新標簽中打開)
我必須使用什么代碼????????????? 解決我的問題的地方?這是代碼:
主頁.html
<html>
<head>
</head>
<body>
<form name="load">yes OR no<input name="id" type="text">
<input type="button" value="OK" onClick="my(document.forms.load)">
<script>
function my(form) {
if (form.id.value=="yes") {
?????????????
} else {
alert("NO")
}
}
</script>
</body>
</html>
b.js
document.write(`
<html>
<h1> Load the HTML code</h1>
</html>
`);
我嘗試使用 <script src="b.js"></script>,但這段代碼立即加載“b.js”,我不知道這是什么
我試著用
var scriptTag = document.createElement('script');
scriptTag.setAttribute('src','b.js');
document.head.appendChild(scriptTag)
但不作業
我試著用
document.getElementsByTagName('body')[0].innerHTML = "<script src='b.js'></script>";
但是這段代碼不起作用
單擊按鈕后,我該怎么做才能加載“b.js”檔案?
謝謝 :)
uj5u.com熱心網友回復:
這就是你要找的嗎?
function my(form) {
const loadHTML = () => {
const scriptB = document.createElement('script')
scriptB.setAttribute('src','b.js')
console.log(scriptB)
document.write(`
<h1>Load the HTML code</h1>
${scriptB.outerHTML}
`);
}
if (form.id.value==="yes") {
loadHTML()
} else {
alert("NO")
}
}
<html>
<head>
</head>
<body>
<form name="load">
<h2>YES or NO</h2>
<input name="id" type="text">
<input type="button" value="OK" onClick="my(document.forms.load)">
</form>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/433885.html
標籤:javascript html 功能 按钮
上一篇:通過a-tag按鈕選擇下拉值
