如何将 HTML 文件加载到 HTML 中,将 JS 与按钮一起使用

How to load HTML file into HTML use JS with button

我用按钮和功能将 b.js 文件加载到 HTML。当用户输入“是”并单击“确定”按钮时,“b.js”必须加载到页面中(不能在新 window 或新选项卡中打开)

我必须在 ???????????? 中使用什么代码?解决我问题的地方? 这是代码:

HOME.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”文件?

谢谢:)

这是您要找的吗?

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>