开发者

Random behaviour of anchor tags

开发者 https://www.devze.com 2023-01-24 23:31 出处:网络
I have an anchor tag in my HTML that is used for a javascript event. The code looks something like this

I have an anchor tag in my HTML that is used for a javascript event. The code looks something like this

<a href="#" onclick="myScript()">run</a>

However, when I click the script, the first time the links is clicked it reloads the page. The second and subsequent clicks, it executes the javascript.

My first thought was that it was something to do with the URL

My URL is

http://localhost/mypage.php?someid=1234567

But, after clicking the javascript link for the first time, it changes to

开发者_StackOverflow社区
http://localhost/mypage.php#

My only solution at the moment is to change the anchor tag to a span, and mimic the behaviour using javascript and css, however this is a hacked solution.

Why would this be happening? Is there a way to prevent this?

My application is a Java Struts application, and it also runs inside a frame.


The reason is that you don't stop the default action of the link, which is to go to the URL #. Return false from the event:

<a href="#" onclick="myScript();return false;">run</a>


or make the href javascript:; instead of #

<a href="javascript:;" onclick="myScript();">run</a>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号