Technique Analysis: Translating external CSS Hyperlink styles with JavaScript

Source: Internet
Author: User
Css|javascript| Tips | links | The CSS Property selector allows you to selectively control the link style, such as having all external links with a small icon to identify it as an external link.

But using CSS has drawbacks:
1, only support Firefox and other Web standards to support a good browser.
2, can only judge the link, cannot judge anchor point or JavaScript. If encountered <a href= "javascript:void (0);" > Can do nothing about it.

This can be combined with JS to complete, first write a style:
A.other:link,a.other:visited,a.other:active
{
Background:url ("External.gif") no-repeat top right;
padding-right:15px;
}
Then write a JS, but JS to take into account the diversity of links, such as the above mentioned JavaScript, anchor points and so on. If it is a picture link, do not apply the style.
<script type= "Text/javascript" >
Window.onload = function ()
{
var alist = document.getElementsByTagName (' a ');
var icount = alist.length;
for (var i = 0;i<icount;i++)
{

if (!chkmylink (alist[i].href,alist[i].innerhtml))
{
Alist[i].classname = ' other ';
}
}
}

S is the link url,innerhtml is the link text
function Chkmylink (s,innerhtml)
{
if (Innerhtml.replace (/^\s*/, ""). Match (/^\var reg =/^http\:\/\//gi;
if (S.match (REG))
{
reg =/^http\:\/\/www.webjx.com/gi;
if (S.match (REG))
{
return true;
}
Else
{
return false;
}
}
return true;
}
</script>
Now you can see the effect.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.