嵌入 javascript 的 html
<html>
<head>
<title>Example</title>
<script type ="text/javascript">
我正确调用了这个函数吗???
function displayatts()
{
document.getElementById("buttonone").onclick = saysomething;
}
这里有什么问题吗???
function saysomething()
{
alert("I am 28 years old");
}
</script>
<body>
<input type="button" id="buttonone" value="me" >
</body>
</head>
</html>
最佳答案
这是完整的示例。
这肯定会起作用。 如果你愿意的话可以测试一下。 将以下代码复制粘贴到W3Schools测试环境中,“编辑并点击我”进行测试。
http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type ="text/javascript">
function saysomething()
{
alert("I am 29 years old");
}
function displayatts()
{
document.getElementById("buttonone").onclick = saysomething;
}
</script>
<body onload="displayatts();">
<input type="button" id="buttonone" value="me" >
</body>
</head>
</html>
关于javascript - 警报消息不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11980211/