开发者

How to addClass as per the link content?

I have one content page where there are links to different documents like .doc, .docx, .ppt .pptx, .txt.

I want to apply/add class dynamically not manual. like as per the link content.

<h3><a href="document.pdf" class="pdf-file">Document.pdf</a></h3>
.pdf-file {
background:url(../images/pdf-file.png) right center no-repeat; padding-right:18px;
}

This is manual class I applied.

jQuery solution will be prefered.

If you like my Question开发者_运维技巧 fav it. :) thank


I know this isn't exactly what you asked for, but why not do this:

a[href*=".pdf"].icon {
    background:url(../images/pdf-file.png) right center no-repeat; padding-right:18px;
}

This way you don't technically need to use JavaScript, the CSS is intelligent enough to interrogate the href attribute and apply the appropriate CSS properties.

Consider having a look at this article.


Try this:

$(document).ready(function() {
  $('a[@href$=".pdf"]').addClass('pdflink');
  $('a[@href$=".doc"]').addClass('doclink');
  $('a[@href$=".docx"]').addClass('docxlink');
  $('a[@href$=".ppt"]').addClass('pptlink');
  //etc
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜