我想使用函数编写以下代码:
<!DOCTYPE html>
<html>
<head>
<title> Test Java Script</title>
</head>
<body>
<button onclick=this.innerHTML=Date();> The Time Is: ? </button>
</body>
</html>
所以我创建了一个函数并传递“this”作为参数,但它不起作用:
<!DOCTYPE html>
<html>
<head>
<title> Test Java Script</title>
</head>
<body>
<button onclick=setTime(this)> The Time Is: ? </button>
</body>
<script type="text/javascript">
function setTime(Object b) {
b.innerHTML=Date();
}
</script>
</html>
我做错了什么?
最佳答案
试试这个:
<!DOCTYPE html>
<html>
<head>
<title> Test Java Script</title>
<script type="text/javascript">
function setTime(b) {
b.innerHTML=Date();
}
</script>
</head>
<body>
<button onclick="setTime(this);"> The Time Is: ? </button>
</body>
</html>
为 onclick 添加引号。 从函数声明中删除“对象”。 单击按钮。
关于javascript - 如何将按钮对象作为函数参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33696058/