admin管理员组文章数量:1026900
I am learning to code JavaScript. In this script, I want a link to not go to its specified location, but to change the text in the paragraph before it. I have created a function to achieve it onclick
, but the function is not able to return false.
<html>
<head>
<title>Javascript Test #6</title>
<script type="text/javascript">
document.getElementById("links").onclick=writeData();
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html"> Click here to change the paragraph content </a>
</body>
</html>
I am learning to code JavaScript. In this script, I want a link to not go to its specified location, but to change the text in the paragraph before it. I have created a function to achieve it onclick
, but the function is not able to return false.
<html>
<head>
<title>Javascript Test #6</title>
<script type="text/javascript">
document.getElementById("links").onclick=writeData();
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html"> Click here to change the paragraph content </a>
</body>
</html>
Share
Improve this question
edited Jan 17, 2023 at 8:13
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Nov 18, 2011 at 7:54
Supratim NayakSupratim Nayak
11 silver badge2 bronze badges
2 Answers
Reset to default 3document.getElementById("links").onclick=writeData;
Don't use the brackets. They force your function to evaluate. but you want to assign the function.
EDIT:
Of Course, you need to Assign your Function after the Document has been loaded. The way it is right now, there will be no element with id links
when the assignment is executed. Basically, just put your script block below the links
element.
<html>
<head>
<title>Javascript Test #6</title>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html"> Click here to change the paragraph content </a>
<script type="text/javascript">
document.getElementById("links").onclick=writeData;
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</body>
</html>
EDIT (Using window.onload):
<html>
<head>
<title>Javascript Test #6</title>
<script type="text/javascript">
window.onload = function() {
document.getElementById("links").onclick=writeData;
}
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html"> Click here to change the paragraph content </a>
</body>
</html>
When dealing with document.getElementById, you should ensure that at the time of the call, the elements are in DOM. Just use an inline click handler:
<html>
<head>
<title>Javascript Test #6</title>
<script type="text/javascript">
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html" onclick="return writeData();"> Click here to change the paragraph content </a>
</body>
</html>
Or, change the order of things: first have the DOM ready, and after that , insert you js script:
<html>
<head>
<title>Javascript Test #6</title>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html" onclick="return writeData();"> Click here to change the paragraph content </a>
<script type="text/javascript">
document.getElementById("links").onclick=writeData;
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</body>
</html>
see it in action here: http://jsfiddle/2CpcF/
I am learning to code JavaScript. In this script, I want a link to not go to its specified location, but to change the text in the paragraph before it. I have created a function to achieve it onclick
, but the function is not able to return false.
<html>
<head>
<title>Javascript Test #6</title>
<script type="text/javascript">
document.getElementById("links").onclick=writeData();
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html"> Click here to change the paragraph content </a>
</body>
</html>
I am learning to code JavaScript. In this script, I want a link to not go to its specified location, but to change the text in the paragraph before it. I have created a function to achieve it onclick
, but the function is not able to return false.
<html>
<head>
<title>Javascript Test #6</title>
<script type="text/javascript">
document.getElementById("links").onclick=writeData();
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html"> Click here to change the paragraph content </a>
</body>
</html>
Share
Improve this question
edited Jan 17, 2023 at 8:13
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Nov 18, 2011 at 7:54
Supratim NayakSupratim Nayak
11 silver badge2 bronze badges
2 Answers
Reset to default 3document.getElementById("links").onclick=writeData;
Don't use the brackets. They force your function to evaluate. but you want to assign the function.
EDIT:
Of Course, you need to Assign your Function after the Document has been loaded. The way it is right now, there will be no element with id links
when the assignment is executed. Basically, just put your script block below the links
element.
<html>
<head>
<title>Javascript Test #6</title>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html"> Click here to change the paragraph content </a>
<script type="text/javascript">
document.getElementById("links").onclick=writeData;
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</body>
</html>
EDIT (Using window.onload):
<html>
<head>
<title>Javascript Test #6</title>
<script type="text/javascript">
window.onload = function() {
document.getElementById("links").onclick=writeData;
}
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html"> Click here to change the paragraph content </a>
</body>
</html>
When dealing with document.getElementById, you should ensure that at the time of the call, the elements are in DOM. Just use an inline click handler:
<html>
<head>
<title>Javascript Test #6</title>
<script type="text/javascript">
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html" onclick="return writeData();"> Click here to change the paragraph content </a>
</body>
</html>
Or, change the order of things: first have the DOM ready, and after that , insert you js script:
<html>
<head>
<title>Javascript Test #6</title>
</head>
<body>
<p id="para">Unchanged Paragraph Content</p>
<br/>
<a id="links" href="javascript-return.html" onclick="return writeData();"> Click here to change the paragraph content </a>
<script type="text/javascript">
document.getElementById("links").onclick=writeData;
function writeData()
{
document.getElementById("para").innerHTML="Changed Paragraph Content";
return false;
}
</script>
</body>
</html>
see it in action here: http://jsfiddle/2CpcF/
本文标签:
版权声明:本文标题:javascript - How to make a link change the content of a paragraph on click? return false not working - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745655414a2161570.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论