如何在HTML中将参数发送到javascript函数? 函数changetext(mypara) { mypara.innerHTML=“oops!”; } var mypara=document.getElementById(“para1”); 单击此文本可更改以下段落的内容 这是我想更改的段落
我想让用户点击标题来改变段落的内容,但我不知道正确的编码方式。如何在HTML中将“mypara”参数发送到myFunction()?您的示例几乎可以正常工作-问题是当您执行这一行时:如何在HTML中将参数发送到javascript函数? 函数changetext(mypara) { mypara.innerHTML=“oops!”; } var mypara=document.getElementById(“para1”); 单击此文本可更改以下段落的内容 这是我想更改的段落,javascript,parameters,Javascript,Parameters,我想让用户点击标题来改变段落的内容,但我不知道正确的编码方式。如何在HTML中将“mypara”参数发送到myFunction()?您的示例几乎可以正常工作-问题是当您执行这一行时: <!DOCTYPE html> <html> <head> <script> function changetext(mypara) { mypara.innerHTML="Ooops!"; } </script> </head> &
<!DOCTYPE html>
<html>
<head>
<script>
function changetext(mypara)
{
mypara.innerHTML="Ooops!";
}
</script>
</head>
<body>
<script>var mypara = document.getElementById("para1");</script>
<h1 onclick="changetext(mypara)">Click this text to change the content of following paragraph</h1>
<p id="para1"> this is a paragraph I would like to change </p>
</body>
</html>
您引用的元素尚不存在。您可以通过内联方式进行修复:
var mypara = document.getElementById("para1");
此方法的实例:当您试图获取元素时,该元素还不存在。
为什么不直接在事件处理程序中获取它呢
function changetext(mypara)
{
document.getElementById(mypara).innerHTML="Ooops!";
}
函数changetext(){
document.getElementById(“para1”).innerHTML=“Ooops!”;
}
单击此文本可更改以下段落的内容
这是我想更改的段落
Jamiec,你真的很有帮助!非常感谢。
<h1 onclick="changetext('para1')">...</h1>
function changetext(mypara)
{
document.getElementById(mypara).innerHTML="Ooops!";
}
<!DOCTYPE html>
<html>
<head>
<script>
function changetext() {
document.getElementById("para1").innerHTML = "Ooops!";
}
</script>
</head>
<body>
<h1 onclick="changetext()">Click this text to change the content of following paragraph</h1>
<p id="para1">this is a paragraph I would like to change</p>
</body>
</html>