最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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/

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far