dongwen2162
2016-09-30 08:53 阅读 21

如果具有特定的类或ID,则将查询字符串添加到Image Source(img src)

I want this in either PHP or JS. Its like, if img has particular class them a query string will be appended to its source url. Lets assume, the particular class is 'hello' & query string is '?source=fb' So, if img has class 'hello' -

<img class="hello" src="http://example.com/1.jpg?source=fb">

The PHP should check & append it to the img src.

Any function that can do this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    doupao6698 doupao6698 2016-09-30 08:56

    $('img.hello').attr('src',$('img.hello').attr('src')+"addedstring")
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <img class="hello" src="http://example.com/1.jpg?source=fb">

    Use like this

    </div>
    
    点赞 评论 复制链接分享
  • dqwh0109 dqwh0109 2016-09-30 08:55

    You can use the attr() method to achieve this by providing a function to amend the current attribute value. Try this:

    $('.hello').attr('src', function(i, src) {
        return src + '?source=fb';
    });
    
    点赞 评论 复制链接分享
  • doutuoji8418 doutuoji8418 2016-09-30 09:16
    You can try this by Jquery. Just give another class to your image tag, assume the class 
    given is "abc", it should be like :
    
    <img class="abc hello" src="http://example.com/1.jpg">
    
    <script>
    
      var image_src = $('.abc').attr('src');
    
      if($('.abc').hasClass('hello')){
         $('.abc').append(image_src+'?source=fb');
      }
    

    点赞 评论 复制链接分享

相关推荐