【问题标题】:How to addClass as per the link content?如何根据链接内容添加类?
【发布时间】:2010-01-18 09:55:11
【问题描述】:

我有一个内容页面,其中包含指向不同文档的链接,例如 .doc、.docx、.ppt .pptx、.txt。

我想动态应用/添加类而不是手动。就像链接内容一样。

<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;
}

这是我申请的手动类。

jQuery 解决方案将是首选。

如果你喜欢我的问题喜欢它。 :) 谢谢

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    我知道这不是您所要求的,但为什么不这样做:

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

    这样,您在技术上不需要使用 JavaScript,CSS 足够智能,可以询问 href 属性并应用适当的 CSS 属性。

    考虑使用look at this article

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 2012-05-12
        • 2021-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-07
        • 1970-01-01
        • 1970-01-01
        • 2016-09-26
        相关资源
        最近更新 更多