最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML page keeps reloading after click event - Stack Overflow

programmeradmin1浏览0评论

Hi I have a small website, which keeps reloading after a click event. How can I prevent this?

PS: I'm only allowed to use pure JS.

HTML

Cookie Clicker

</head>
<body>
    <header>
        <h1>Points: </h1>
        <span id="score"></span>
    </header>
    <div class="container">
        <div>
            <a href="" onclick="addPoints(1)">
                <img src="assets/graphics/Friis.png" />
            </a>
        </div>
    </div>

    <script src="assets/scripts/cookieClicker.js"></script>
</body>
</html>


JS

var points = 0,
    score = document.getElementById("score");

function addPoints(increase) {
    console.log('hit');
    points += increase;

    score.innerHTML = '' + points;
};

Hi I have a small website, which keeps reloading after a click event. How can I prevent this?

PS: I'm only allowed to use pure JS.

HTML

Cookie Clicker

</head>
<body>
    <header>
        <h1>Points: </h1>
        <span id="score"></span>
    </header>
    <div class="container">
        <div>
            <a href="" onclick="addPoints(1)">
                <img src="assets/graphics/Friis.png" />
            </a>
        </div>
    </div>

    <script src="assets/scripts/cookieClicker.js"></script>
</body>
</html>


JS

var points = 0,
    score = document.getElementById("score");

function addPoints(increase) {
    console.log('hit');
    points += increase;

    score.innerHTML = '' + points;
};
Share Improve this question asked Sep 1, 2015 at 8:17 Michael Tot KorsgaardMichael Tot Korsgaard 4,01411 gold badges56 silver badges93 bronze badges 2
  • 1 possible duplicate of How to prevent reload with onclick without "#"? – JJJ Commented Sep 1, 2015 at 8:20
  • 1 Change href to href="javascript:;" or pass the event and call preventDefault() – haim770 Commented Sep 1, 2015 at 8:21
Add a ment  | 

4 Answers 4

Reset to default 6

You can modify only your html and it works:

<a href="javascript:void(0);" onclick="addPoints(1)">

Some people make this:

<a href="#" onclick="addPoints(1)">

But this is an anchor that scrolls your page to top. If you need to make by javascript, search about event preventDefault.

EDIT

Read about void javascript function:

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/void

It's good to understand behaviour.

update your code to:

<a href="" onclick="addPoints(1);return false">

or

<a href="" onclick="return addPoints(1)">

and let your function addPoints return false. Upside of this is that you can actually let the href link to a page in case addPoints reaches a certain level for instance. If addPoints returns true the link will take the user to that page, return false will keep the user on the current page.

Instead of using onClick you can just change the href to run the function. For example:

<a href="javascript:addPoints(1);">

that happens because you code contain

   <a href="" onclick="addPoints(1)"> // These Can Reload Same Page
   <a href="#" onclick="addPoints(1)"> // Modify upper code to these code
发布评论

评论列表(0)

  1. 暂无评论