Jquery:编辑href列表

时间:2014-11-09 12:06:16

标签: jquery html

我有一个链接列表,我想编辑此列表的所有href。 例如:

<a class="repo-avatar-link" href="/damienmarchand/sqdsdqsdsfds">

我想:

<a class="repo-avatar-link" href="http://something/damienmarchand/sqdsdqsdsfds">

我的页面:

<ul class="repository-list" data-current-status="all">
  
    
    <li class="iterable-item">
      <span class="aui-avatar aui-avatar-medium aui-avatar-project repo-avatar">
        <span class="aui-avatar-inner">
          <a class="repo-avatar-link" href="/damienmarchand/sqdsdqsdsfds">
            <img src="https://d3oaxc4q5k2d6q.cloudfront.net/m/5fe8c0346b2d/img/language-avatars/default_32.png" alt="" data-modules="utils/retina">
          </a>
        </span>
      </span>
      <span class="repo-name">
        <a class="execute" href="/damienmarchand/sqdsdqsdsfds">sqdsdqsdsfds</a>
        
      </span>
      <span class="last-updated">
        
          Updated <time datetime="2014-11-07T23:25:52.716869+00:00">2014-11-07</time>
        
      </span>
    </li>
  
    
    <li class="iterable-item">
      <span class="aui-avatar aui-avatar-medium aui-avatar-project repo-avatar">
        <span class="aui-avatar-inner">
          <a class="repo-avatar-link" href="/damienmarchand/sdqdsd">
            <img src="https://d3oaxc4q5k2d6q.cloudfront.net/m/5fe8c0346b2d/img/language-avatars/java_32.png" alt="" data-modules="utils/retina">
          </a>
        </span>
      </span>
      <span class="repo-name">
        <a class="execute" href="/damienmarchand/sdqdsd">sdqdsd</a>
        
      </span>
      <span class="last-updated">
        
          Updated <time datetime="2014-11-07T23:25:21.619545+00:00">2014-11-07</time>
        
      </span>
    </li>
  
    
    <li class="iterable-item">
      <span class="aui-avatar aui-avatar-medium aui-avatar-project repo-avatar">
        <span class="aui-avatar-inner">
          <a class="repo-avatar-link" href="/damienmarchand/test-public">
            <img src="https://d3oaxc4q5k2d6q.cloudfront.net/m/5fe8c0346b2d/img/language-avatars/c_sharp_32.png" alt="" data-modules="utils/retina">
          </a>
        </span>
      </span>
      <span class="repo-name">
        <a class="execute" href="/damienmarchand/test-public">test public</a>
        
      </span>
      <span class="last-updated">
        
          Updated <time datetime="2014-11-05T22:13:57.822553+00:00">2014-11-05</time>
        
      </span>
    </li>
  
</ul>

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您只需找到相关代码,然后为每个代码使用attr()获取并更新每个代码的href属性:

&#13;
&#13;
// find a tags within elements with class .repository-list
$('.repository-list a').each(function() {
  $(this).attr('href', 'http://something' + $(this).attr('href'));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul class="repository-list" data-current-status="all">
  
    
    <li class="iterable-item">
      <span class="aui-avatar aui-avatar-medium aui-avatar-project repo-avatar">
        <span class="aui-avatar-inner">
          <a class="repo-avatar-link" href="/damienmarchand/sqdsdqsdsfds">
            <img src="https://d3oaxc4q5k2d6q.cloudfront.net/m/5fe8c0346b2d/img/language-avatars/default_32.png" alt="" data-modules="utils/retina">
          </a>
        </span>
      </span>
      <span class="repo-name">
        <a class="execute" href="/damienmarchand/sqdsdqsdsfds">sqdsdqsdsfds</a>
        
      </span>
      <span class="last-updated">
        
          Updated <time datetime="2014-11-07T23:25:52.716869+00:00">2014-11-07</time>
        
      </span>
    </li>
  
    
    <li class="iterable-item">
      <span class="aui-avatar aui-avatar-medium aui-avatar-project repo-avatar">
        <span class="aui-avatar-inner">
          <a class="repo-avatar-link" href="/damienmarchand/sdqdsd">
            <img src="https://d3oaxc4q5k2d6q.cloudfront.net/m/5fe8c0346b2d/img/language-avatars/java_32.png" alt="" data-modules="utils/retina">
          </a>
        </span>
      </span>
      <span class="repo-name">
        <a class="execute" href="/damienmarchand/sdqdsd">sdqdsd</a>
        
      </span>
      <span class="last-updated">
        
          Updated <time datetime="2014-11-07T23:25:21.619545+00:00">2014-11-07</time>
        
      </span>
    </li>
  
    
    <li class="iterable-item">
      <span class="aui-avatar aui-avatar-medium aui-avatar-project repo-avatar">
        <span class="aui-avatar-inner">
          <a class="repo-avatar-link" href="/damienmarchand/test-public">
            <img src="https://d3oaxc4q5k2d6q.cloudfront.net/m/5fe8c0346b2d/img/language-avatars/c_sharp_32.png" alt="" data-modules="utils/retina">
          </a>
        </span>
      </span>
      <span class="repo-name">
        <a class="execute" href="/damienmarchand/test-public">test public</a>
        
      </span>
      <span class="last-updated">
        
          Updated <time datetime="2014-11-05T22:13:57.822553+00:00">2014-11-05</time>
        
      </span>
    </li>
  
</ul>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我建议:

// select the appropriate elements, set the href attribute:
$('.repository-list a').attr('href', function (i,h) {
  // i is the index of the current element in the returned collection,
  // h is the current value of the attribute we're modifying.

  // we return the string '/something' joined with the string of the current href:
  return '/something' + h;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="repository-list" data-current-status="all">
  
    
    <li class="iterable-item">
      <span class="aui-avatar aui-avatar-medium aui-avatar-project repo-avatar">
        <span class="aui-avatar-inner">
          <a class="repo-avatar-link" href="/damienmarchand/sqdsdqsdsfds">
            <img src="https://d3oaxc4q5k2d6q.cloudfront.net/m/5fe8c0346b2d/img/language-avatars/default_32.png" alt="" data-modules="utils/retina">
          </a>
        </span>
      </span>
      <span class="repo-name">
        <a class="execute" href="/damienmarchand/sqdsdqsdsfds">sqdsdqsdsfds</a>
        
      </span>
      <span class="last-updated">
        
          Updated <time datetime="2014-11-07T23:25:52.716869+00:00">2014-11-07</time>
        
      </span>
    </li>
  
    
    <li class="iterable-item">
      <span class="aui-avatar aui-avatar-medium aui-avatar-project repo-avatar">
        <span class="aui-avatar-inner">
          <a class="repo-avatar-link" href="/damienmarchand/sdqdsd">
            <img src="https://d3oaxc4q5k2d6q.cloudfront.net/m/5fe8c0346b2d/img/language-avatars/java_32.png" alt="" data-modules="utils/retina">
          </a>
        </span>
      </span>
      <span class="repo-name">
        <a class="execute" href="/damienmarchand/sdqdsd">sdqdsd</a>
        
      </span>
      <span class="last-updated">
        
          Updated <time datetime="2014-11-07T23:25:21.619545+00:00">2014-11-07</time>
        
      </span>
    </li>
  
    
    <li class="iterable-item">
      <span class="aui-avatar aui-avatar-medium aui-avatar-project repo-avatar">
        <span class="aui-avatar-inner">
          <a class="repo-avatar-link" href="/damienmarchand/test-public">
            <img src="https://d3oaxc4q5k2d6q.cloudfront.net/m/5fe8c0346b2d/img/language-avatars/c_sharp_32.png" alt="" data-modules="utils/retina">
          </a>
        </span>
      </span>
      <span class="repo-name">
        <a class="execute" href="/damienmarchand/test-public">test public</a>
        
      </span>
      <span class="last-updated">
        
          Updated <time datetime="2014-11-05T22:13:57.822553+00:00">2014-11-05</time>
        
      </span>
    </li>
  
</ul>