简体   繁体   English

将类添加到所有具有相同文件扩展名的锚标记

[英]add class to all the anchor tag which have same file extention

<table>
  <tr>
    <td>
      <span class="file"><a class="" href="#">test1.docx</a></span>
      <span class="file"><a class="" href="#">test1.pdf</a></span>
    </td>
    <td>
      <span class="file"><a class="" href="#">test1.docx</a></span>
      <span class="file"><a class="" href="#">test1.pdf</a></span>
    </td>
  </tr>
  <tr>
    <td>
      <span class="file"><a class="" href="#">test2.docx</a></span>
      <span class="file"><a class="" href="#">test2.pdf</a></span>
    </td>
    <td>
      <span class="file"><a class="" href="#">test2.docx</a></span>
      <span class="file"><a class="" href="#">test2.pdf</a></span>
    </td>
  </tr>
</table>

<script>
$(document).ready(function(){
  var fileName = $('table td span.file a').text();
  var ext = fileName.text().split('.').pop();
  if(ext == pdf) {
    $(this).addClass('pdf');
  }
});
</script>

The purpose of the above code is to add class (class='pdf') to anchor tag, which have file extension as 'pdf'. 上面代码的目的是向锚标记添加类(class ='pdf'),锚标记的文件扩展名为'pdf'。 As this code is dynamically generated, I've no access to modify it. 由于此代码是动态生成的,因此我无权对其进行修改。 So, I decided to write a jQuery code. 因此,我决定编写一个jQuery代码。 I've messed up something with the above code and it is not giving me desired output. 我把上面的代码弄乱了,它没有给我想要的输出。

Please help. 请帮忙。

You have to iterate, right now this is the document, not each anchor 您必须进行迭代,现在this是文档,而不是每个锚点

$(document).ready(function(){
  $('table td span.file a').each(function() {

      var ext = $(this).text().split('.').pop();
      if(ext == 'pdf') {
          $(this).addClass('pdf');
      }
  });
});

FIDDLE 小提琴

A more sneaky way to do this would be to just return the extension as the class 一个更狡猾的方法是将扩展名作为类返回

$('table td span.file a').addClass(function() {
    return $(this).text().split('.').pop();
});

That way you're setting pdf, docx etc. classes on the anchors automagically 这样,您可以自动在锚点上设置pdf,docx等类

FIDDLE 小提琴

An other way: 其他方式:

$(document).ready(function () {
    $('table td span.file a').addClass(function () {
        return $(this).text().split('.').pop() == "pdf" ? "pdf" : null;
    });
});

--DEMO-- --DEMO--

Thx @adeneo for jsFiddle base sample Thx @adeneo for jsFiddle基本示例

BTW, you could add class regarding any extension: {oops, already posted by adeneo...} 顺便说一句,您可以添加有关任何扩展名的类: {oops,已经由adeneo发布...}

 $(document).ready(function () { $('table td span.file a').addClass(function () { return $(this).text().split('.').pop(); }); }); 

--DEMO-- --DEMO--

this will help you. 这将为您提供帮助。

$(document).ready(function(){
  $('table td span.file a').each(function() {

      var ext = $(this).text().split('.').pop(); 
      if(ext == pdf) {
          $(this).addClass('pdf');
      }
      else if(ext == docx)
      {
          $(this).addClass('docx');
      }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM