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

html - Javascript and Anchor Tags, Best Practice? - Stack Overflow

programmeradmin8浏览0评论

Quick Question.

Should we put Javascript in the HREF or use onClick (event)?

Are there any pros/cons to using either one. Personally I think it's easier/cleaner to simply put code in the href, and not have to return false or worry about jumping to #

Are there any known browser specific issues with each method...

Examples:

<a href="javascript: alert('foo!');">Click Me</a>
<a href="#" onClick="alert('foo!');return false">Click Me</a>
<a href="javascript:void(0)" onclick="alert('foo!');">Click Me</a>

Quick Question.

Should we put Javascript in the HREF or use onClick (event)?

Are there any pros/cons to using either one. Personally I think it's easier/cleaner to simply put code in the href, and not have to return false or worry about jumping to #

Are there any known browser specific issues with each method...

Examples:

<a href="javascript: alert('foo!');">Click Me</a>
<a href="#" onClick="alert('foo!');return false">Click Me</a>
<a href="javascript:void(0)" onclick="alert('foo!');">Click Me</a>
Share Improve this question edited Jul 22, 2011 at 14:18 Weston Watson asked Jul 22, 2011 at 14:12 Weston WatsonWeston Watson 5,7246 gold badges25 silver badges25 bronze badges 1
  • 2 You should externalize javascript code. Hear for the click event of the a tag – chchrist Commented Jul 22, 2011 at 14:15
Add a comment  | 

5 Answers 5

Reset to default 12

1) Rather use onclick. Be sure to have a rational fallback URL in href when JS is not allowed.

2) Rather than onclick="...", you should use event handler. Find elements using jQuery or XPath, and then for each, call element.addEventListener().

element.addEventListener("click", function() { alert("bar"); }, false);

Or the old way...

element.onclick = function() { alert("foo"); };

Keeping the code separate from the html is cleaner IMO.

<a id="foo" href="#">Click Me</a>

Then in head or separate js file:

document.getElementByID("foo").onclick = function() { alert("hi"); }

I would personally not put the JavaScript code in the HTML. You should use an event listener that will get triggered when the <a> is clicked on.

<a href="#" id="linkA">Click Me</a>

And then:

document.getElementById('linkA').addEventListener('click', function(e){
    e.preventDefault(); // Prevents page from scrolling to the top
    alert('foo!');
});

In the onclick event (although assigned with JS rather then the attribute) with a sensible href attribute. Build on things that work

Generally, I would externalize JavaScript but use thehref to call it:

<a href="javascript:foo('bar!');">Click Me</a>

Although I think your quick alert example is fine as an exception to the rule.

However, if your using a JS library like jQuery you can of course assign events directly - see the first example in jQuery tutorial which looks at the anchor element http://docs.jquery.com/Tutorials:How_jQuery_Works

发布评论

评论列表(0)

  1. 暂无评论