Implementing selector tags in IE6 via jquery

Source: Internet
Author: User

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.

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.