admin管理员组

文章数量:1315034

Ok, so I'm quite new to jQuery, but found this bizzare problem just now,

If we ignore jQuery for a second and consider this scenario, if I have two links like below both with an href and both with and onclick event. The first link will not follow the href because the onclick returns false, and the second link will because the onclick returns true.

<a href="/page.html" onclick="return false;">Dont follow</a>

<a href="/page.html" onclick="return true;">Follow</a>

This works just hunky dory in every browser as it should, the thing is, as soon as I include the jQuery script on the page this stops working in all versions of IE which then always follows the href whether the onclick returns false or not. (it continues to work fine in other browsers)

Now if I add an event using jQuery and call .preventDefault() on the event object instead of doing it the old fashioned way this behaves correctly, and you may say, well just do that then? But I have a site with thousands of lines of code and I am adding jQuery support, I don't want to run the risk that I might miss an already defined html onclick="" and break the web site.

I cant see why jQuery should prevent perfectly normal JavaScript concepts from working, so is this a jQuery bug or am I missing something?

Ok, so I'm quite new to jQuery, but found this bizzare problem just now,

If we ignore jQuery for a second and consider this scenario, if I have two links like below both with an href and both with and onclick event. The first link will not follow the href because the onclick returns false, and the second link will because the onclick returns true.

<a href="/page.html" onclick="return false;">Dont follow</a>

<a href="/page.html" onclick="return true;">Follow</a>

This works just hunky dory in every browser as it should, the thing is, as soon as I include the jQuery script on the page this stops working in all versions of IE which then always follows the href whether the onclick returns false or not. (it continues to work fine in other browsers)

Now if I add an event using jQuery and call .preventDefault() on the event object instead of doing it the old fashioned way this behaves correctly, and you may say, well just do that then? But I have a site with thousands of lines of code and I am adding jQuery support, I don't want to run the risk that I might miss an already defined html onclick="" and break the web site.

I cant see why jQuery should prevent perfectly normal JavaScript concepts from working, so is this a jQuery bug or am I missing something?

Share Improve this question edited Aug 8, 2022 at 21:35 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 12, 2010 at 21:04 3urdoch3urdoch 7,3428 gold badges44 silver badges59 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

The most likely cause is that you have a syntax error elsewhere in the page that is preventing javascript from running properly on the page. Turn on script error notifications in your browser to make sure it isn't hiding errors from you.

Try also:

onClick = "event.returnValue = false; return false;"

I corrected your sample code for the second link and tested in IE 8 and it works fine. I'm willing to bet JohnFx is right and you have some other error on your page stopping it from working.

I tested using an included jquery library as well.

Sample:

   <html>
 <head>
  <script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.4/jquery.min.js"></script> 
 </head>
 <body>
   <a href="/page.html" onclick="return false;">Dont follow</a>
   <a href="/page.html" onclick="return true;">Follow</a>
 </body>
    </html>

本文标签: