2 回答
TA贡献1847条经验 获得超11个赞
html中调用javascript函数基本有两种方法:
通过元素的事件来调用javascript函数
<html><head><title>javascript函数调用</title></head><body><buttononclick="demo()">点击调用demo函数</button><script>function demo(){alert("调用成功");}</script></body><html>效果图
使用addEventListener调用函数
<html><head><title>javascript函数调用</title></head><body><button id="btn">使用addEventListener调用函数</button><script> document.getElementById("btn").addEventListener("click",demo); function demo() { alert("addEventListener调用函数成功"); }</script></body></html> |
以上就是通过HTML调用javascript函数,分别为DOM级和DOM2级函数调用,还有DOM3函数调用,但是与DOM2级函数差别不大,就不做讲解。在实际开发中DOM2级函数调用更多的被使用。
TA贡献2036条经验 获得超8个赞
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> new document </title>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
<script language="javascript" type="text/javascript">
function getp1(){
alert(document.getElementById("p1").innerHTML);
}
</script>
</head>
<body>
<p id="p1">段落</p>
<input type="button" onclick="getp1()" value="获取P1"/>
</body>
</html>
添加回答
举报


