2016-09-30 2 views
-1

PHPまたはJSのいずれかに入力します。そのように、もしimgが特定のクラスを持っているなら、クエリ文字列がそのソースurlに追加されます。特定のクラスは、だから、「ハロー」&クエリ文字列は「?ソース= FB」 あるIMGは、クラスのハロー」を持っている場合と仮定しましょう、 -特定のクラスまたはIDを持つ画像ソース(img src)にクエリ文字列を追加します。

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

PHPは&はIMG srcにそれを追加し確認する必要があります。

これを行う機能はありますか?

+1

のように使用したい正確に理解するためにいくつかの詳細を提供します。 –

答えて

1

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

この

+0

このメソッドには注意してください。複数の '.hello'要素を持っているかのように、すべての要素の結合された' src'を互いに関連付けます。 –

+0

@RoryMcCrossanはいこれは、クラス – guradio

+1

はい、それは仕事をします。ありがとうございます:-) –

2

attr()メソッドを使用するには、現在の属性値を修正する関数を使用します。これを試してみてください:

$('.hello').attr('src', function(i, src) { 
    return src + '?source=fb'; 
}); 
+0

はい、これは動作します。ありがとう:-) –

0
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'); 
    } 

関連する問題