In the previous CSS property selector making personalized link style, we used CSS selector tags to implement the related functionality. But the CSS selector is supported by most browsers, but not damned IE6 support, then how to do? Yes, we can do it through jquery.
As you can see, the style implemented using jquery is exactly the same as using the CSS selector, and it is supported in IE6.
The principle is simple-match the conditions with the jquery selector, and then dynamically add a class to the link. For example, jquery matches a link to the mailto type, and then we add a mailto class to the link.
jquery's attribute selector is basically consistent with CSS and is similar in usage:
$("a[href^='mailto']").addClass ("mailto");
This is no longer a description of the jquery attribute selector, and if you don't understand it, you can view the official jquery reference documentation.
Style, we just have to change the original selector syntax to class is OK.
The final style:
a {
background:url(a.gif) no-repeat right 4px;
padding-right:18px;color:#369;line-height:24px;
}
a.mailto{background-position:right -242px;}
a.doc{background-position:right -161px}
a.xls{background-position:right -282px}
a.pdf{background-position:right -79px}
a.mp3{background-position:right -204px}
a.swf{background-position:right -120px}
a.rar{background-position:right -38px}
a.home{background-position:right -328px}
The final JS script:
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function(){
$("a[href^='mailto']").addClass("mailto");
$("a[href$='.doc']").addClass("doc");
$("a[href$='.mp3']").addClass("mp3");
$("a[href$='.swf']").addClass("swf");
$("a[href$='.rar']").addClass("rar");
$("a[href$='.pdf']").addClass("pdf");
$("a[href$='.xls']").addClass("xls");
$("a[href*='qianduan.net']").addClass("home");
});
</script>
The HTML code does not need to make any changes.