(3) Select an element -- (7) Add a style for the link (Stylinglinks)

Source: Internet
Author: User
Let & amp; 39; dependencies, asfollows: a {color: 00c;} amailto {backg Let's say we want to have different styles for different types of links. we first define the styles in our stylesheet, as follows: a {color: # 00c;}. mailto {background: url (images/mail.png) no-repeat right top; padding-right: 18px;} Then, we add the three linoleic Sses-mailto, publish link, and henrylink-to the appropriate links using jQuery. to add a class for all e-mail links, we construct a selector that looks for all anchor elements (a) with an href attribute ([href]) that begins with mailto: (^ = "mailto:"), as follows: $ (document ). ready (function () {$ ('a [href ^ = "mailto:"] '). addClass ('mailto');}); Because of the rules defined in the page's stylesheet, an envelope Image appears after all mailto: links on the page, as shown in the following screenshot: to make different links have different colors, we first define the style in the style sheet, as shown below: a {color: # 00c;}. mailto {background: url (images/mail.png) no-repeat right top; padding-right: 18px ;}then, we add three classes -- mailto, consumer link and henrylink -- use jquery to determine the link. To add a class to all email links, we have created a selector and found all the anchor elements with the href attribute, which starts with mailto: (^ = "mailto ") $ (document ). ready (function () {$ ('a [href ^ = "mailto:"] '). addClass ('mailto');}); because of the rules defined in the style sheet on the page, an image of an envelope is displayed after all mailto links on the page, as shown in the following-To add a class for all links to PDF files, we use the dollar sign rather than the caret symbol. this is because we're re selecting links with an href attribute that ends. pdf, as follows: $ (Document ). ready (function () {$ ('a [href ^ = "mailto:"] '). addClass ('mailto'); $ ('a [href $ = ". pdf "] '). addClass ('linked link') ;}); to add a class to all links to the PDF file, we use dollar signs instead of delimiters ^. This is because we are selecting an href link ending with a token, as shown below: $ (document ). ready (function () {$ ('a [href ^ = "mailto:"] '). addClass ('mailto'); $ ('a [href $ = ". pdf "] '). addClass ('privacy link');}); The stylesheet rule for the newly-added extends linkclass causes an Adobe Acrobat icon to appear after each link to a PDF document, as shown in the following screenshot: add an Adobe Acrobat icon after each link to the PDF file, as shown in the screenshot below: Attribute selecto Rs can be combined as well. we can, for example, add a henrylinkclass for all links with an hrefvalue that both starts with httpand contains henry anywhere: $ (document ). ready (function () {$ ('a [href ^ = "mailto:"] '). addClass ('mailto'); $ ('a [href $ = ". pdf "] '). addClass ('hyperlink link'); $ ('a [href ^ = "http"] [href * = "henry"] '). addClass ('henrylink');}); the attribute selector can also be used for connection. For example, we can start with http, in addition, add a henrylink class with the href attribute of henry at a location: $ (Document ). ready (function () {$ ('a [href ^ = "mailto:"] '). addClass ('mailto'); $ ('a [href $ = ". pdf "] '). addClass ('hyperlink link'); $ ('a [href ^ = "http"] [href * = "henry"] '). addClass ('henrylink');}); With the three classes applied to the three types of links, we shocould see the following: after these three classes are added to these three links, we will see the following interface: In the preceding screenshot, note the PDF icon to the right of the Hamletlink, the envelope icon next to t He emaillink, and the white background and black border around the Henry Vlink. in the previous screenshot, you will see the icon of the PDF file on the right side of the Hamlet link, the email image is next to the mail link, and the Henry V link has a white background and a black border.

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.