开发者

Styling anchor tags without href's--being used to trigger Javascript within page [closed]

开发者 https://www.devze.com 2022-12-14 00:09 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. 开发者_StackOverflow中文版
Closed. This question is opinion-based. It is not currently accepting answers.
开发者_StackOverflow中文版

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed last year.

Improve this question

What is the most cross-browser effective way to style anchor tags without href's?

I found this article on making the styles apply in older versions of IE by throwing in an href="#". However, I am applying this about halfway down the page and find that it acts as it should; brings you to the top of the page again. This makes the page 'jump' which is a bad experience for anyone trying to use the bottom half of the page. But...when I take the href's out IE will not apply the css styles.

Thoughts?


Use markup like this...

<a id="myLink" href="JavascriptDisabled.htm">Jump</a>

JavascriptDisabled.htm should be a page that says Javascript must be enabled for this to work!

Then you can override the default click functionality of the link with unobtrusive Javascript like this...

window.onload = function() {
  document.getElementById("myLink").onclick = function() {
    //Execute your Javascript
    return false; // This prevents the redirect
  }
}

So people with Javascript enabled get the correct functionality. Those with Javascript disabled will actually get a message that tells them why it's not working. I'd say it's a win/win!


Use href="javascript:void(0)" in place of #. This is the most common method to get around this problem.

The anchor still gets an href attribute, but it doesn't do anything.

0

精彩评论

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

关注公众号