javascript - How to make a link change the content of a paragraph on click? return false not working - Stack Overflow

I am learning to code JavaScript. In this script, I want a link to not go to its specified location, bu

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
Add a ment  | 

2 Answers 2

Reset to default 3
document.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/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745655416a4638516.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信