将 .each link 包含函数合并为一个 if 语句

Combine .each link contain function to one if statement

我试图在整个页面上查找所有“a”标签,所以我使用了“each”函数。并且想有办法把所有的link包含条件组合成一个js,这样看起来就不会那么乱了。但我想我不能在 if 语句中使用“a[href*="something"]”?如果不是,那会是什么?我试过“indexOf”,但一直给我一个错误,所以在每个里面,不能使用“index of”?

$('a').each(function() {
    if ($('a[href*="typeone"]')) {
      var linktitle = $(this).attr('title');
      $(this).attr('title', 'Typeone: ' + linktitle);
    } else if ($('a[href*="typetwo"]')) {
      var linktitle = $(this).attr('title');
      $(this).attr('title', 'Typetwo: ' + linktitle);
    } else if ($('a[href*="typethree"]')) {
      var linktitle = $(this).attr('title');
      $(this).attr('title', 'Typethree: ' + linktitle);
    } else {
      var linktitle = $(this).attr('title');
      $(this).attr('title', 'Other: ' + linktitle);
    }
    });
<a href="https://typeone/something.html" title="the page name" target="_blank">this is the type one link</a>

<a href="https://typetwo/something.html" title="the page name" target="_blank">this is the type two link</a>

<a href="https://typethree/something.html" title="the page name" target="_blank">this is the type three link</a>

<a href="https://other/something.html" title="the page name" target="_blank">this is the other link</a>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

您可以使用 $(this).attr("href").indexOf.. 来检查 href 是否具有 somevalue,具体取决于此更改您的 title 属性。

演示代码 :

//target only a tag where href present.
$('a[href]').each(function() {
  var linktitle = $(this).attr('title');
  //use indexof
  if ($(this).attr("href").indexOf("typeone") != -1) {
    $(this).attr('title', 'Typeone: ' + linktitle);
  } else if ($(this).attr("href").indexOf("typetwo") != -1) {
    $(this).attr('title', 'Typetwo: ' + linktitle);
  } else if ($(this).attr("href").indexOf("typethree") != -1) {
    $(this).attr('title', 'Typethree: ' + linktitle);
  } else {
    $(this).attr('title', 'Other: ' + linktitle);
  }
});
<a href="https://typeone/something.html" title="the page name" target="_blank">this is the type one link</a>

<a href="https://typetwo/something.html" title="the page name" target="_blank">this is the type two link</a>

<a href="https://typethree/something.html" title="the page name" target="_blank">this is the type three link</a>

<a href="https://other/something.html" title="the page name" target="_blank">this is the other link</a>



<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

您似乎想要匹配主机名。您可以为要匹配的主机创建一个对象并在 attr(function)

内循环

const titleMatch = {
  typeone: 'Typeone',
  typetwo: 'Typetwo',
  typethree: 'Typethree'
}

$('a[title]').attr('title', function(i, curr) { 
  for ([k,v] of Object.entries(titleMatch)) {
    if (this.host.includes(k)) {
      return v + ': ' + curr
    }
  }
  return 'Other: ' + curr
});

// for demo only, display results
$('a').each((i,el)=> console.log(el.title))
<a href="https://typeone/something.html" title="the page name" target="_blank">this is the type one link</a>
<a href="https://typetwo/something.html" title="the page name" target="_blank">this is the type two link</a>
<a href="https://typethree/something.html" title="the page name" target="_blank">this is the type three link</a>
<a href="https://other/something.html" title="the page name" target="_blank">this is the other link</a>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>