如果图像源(img src)具有特定的类或ID,请将查询字符串添加到该图像源


Add query string to Image Source(img src) if it has a particular class or ID

我想在PHP或JS中使用它。类似的是,如果img有特定的类,那么一个查询字符串将附加到它的源url。让我们假设,特定的类是"hello"&查询字符串是'?source=fb'所以,如果img有类"hello"-

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

PHP应该检查&将其附加到img src中。

有什么函数可以做到这一点?

$('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">

像这样使用

您可以使用attr()方法来实现这一点,方法是提供一个修改当前属性值的函数。试试这个:

$('.hello').attr('src', function(i, src) {
    return src + '?source=fb';
});
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');
  }