(3)选择元素——(7)为链接加样式(Styling links)

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; }a.mailto { background: url(images/mail.png) no-repeat right top; padding-right: 18px;}
Then, we add the three classes—mailto, pdflink, 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:

我们想让不同的链接有不同的颜色,我们首先在样式表中定义样式,如下:
a { color: #00c; }a.mailto { background: url(images/mail.png) no-repeat right top; padding-right: 18px;}
然后,我们添加三个类——mailto,pdflink和henrylink——使用jquery确定链接。 为了给所有的email链接加上一个类,我们建立了一个选择器,找到了所有的有着href属性的锚元素,而且是以mailto开头的:(^="mailto")如下:
$(document).ready(function() { $('a[href^="mailto:"]').addClass('mailto');});
由于在页面的样式表中定义的规则,一个信封的图片将显示在页面上所有的mailto链接后面,正如下面的-展示的那样:
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 selecting links with an href attribute that ends with .pdf , as follows: 
$(document).ready(function() { $('a[href^="mailto:"]').addClass('mailto'); $('a[href$=".pdf"]').addClass('pdflink');});
为了给所有到PDF文件的链接加上一个类,我们使用美元符号而不是脱字符号^。这是因为我们正在选择一个href属性是以.pdf结尾的链接,如下:
$(document).ready(function() { $('a[href^="mailto:"]').addClass('mailto'); $('a[href$=".pdf"]').addClass('pdflink');});
The stylesheet rule for the newly-added pdflinkclass causes an Adobe Acrobat icon 
to appear after each link to a PDF document, as shown in the following screenshot:

新添加的pdflink类的样式规则在每一个到PDF文件的链接后面加上了一个Adobe Acrobat图标,正如下面的截屏显示的那样:
Attribute selectors 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('pdflink');
$('a[href^="http"][href*="henry"]') 
.addClass('henrylink');
});
属性选择器也可以连接使用,比如,我们可以为所有以http开头,而且在某个位置含有henry的href属性添加一个henrylink类:
$(document).ready(function() {
$('a[href^="mailto:"]').addClass('mailto');
$('a[href$=".pdf"]').addClass('pdflink');
$('a[href^="http"][href*="henry"]') 
.addClass('henrylink');
});
With the three classes applied to the three types of links, we should see the following:

在这三个类添加到这三种链接以后,我们会看到下面这样的界面:

In the preceding screenshot, note the PDF icon to the right of the Hamletlink, the envelope icon next to the emaillink, and the white background and black border around the Henry Vlink.
在之前的截屏中,你会看到PDF文件的图标在Hamlet链接的右侧,邮件图片在邮件链接的旁边,Henry V链接有着白色的背景和黑色的边框。














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值