如何在 JS 中嵌入 HTML 代码

如题所述

javaScript 方法通过onclick事件触发

嵌入javaScript有两种书写方式:
1、内部js
1.1在script中编写

<script type="text/javascript">
function showOutter(){
// 通过id,获得输入内容
var content = document.getElementById("content").value;
alert(content);
}
</script>
</head>
<body>
<input type="text" id="content" />
<input type="button" value="弹出" onclick="showOutter();"/>
</body>

1.2直接写在标签中
<body>

<h1 onclick="this.innerHTML='hello!'">请点击这段文本!</h1>

</body>

2、外部js
只需在外部建立.js格式文档,将1.1<script>标签之间的代码写入.js文件中。然后
<head>
<script type="text/javascript" src='../**.js'></script>
</head>
<body>
<input type="text" id="content" />
<input type="button" value="弹出" onclick="showOutter();"/>
</body>
温馨提示:答案为网友推荐,仅供参考