さまざまなタグ(img、divなど)を含むサーバーのHTMLページをプルしています。
クライアント側では、これをdivに挿入して、htmlファイルとまったく同じように表示されるようにします。画像やテキストが含まれている場合があります。ただし、srcに相対パスを持つimgタグがある場合があります。
完全なhttp/https(絶対パス)を参照せず、相対パスであることを識別するために「/」で始まるこれらのimgタグを識別するにはどうすればよいですか?
表示に使用しているDIVに識別子がある場合は、次を使用してそのdivの子を取得してみてください。
$("#divID").children().find("img").each(function(){
if(($(this).attr("src")).indexOf("PATH")>0)
{ Do whatever.. }
});
文字列関数/正規表現を使用して、属性がで始まるかどうかを確認しsrc
たり、それに応じて変更したりできます。http
https
/
例:
$("#div1").find("img").length // returns the number of images inside #div1
$("#div1").find("img").each(function () {
var src = $(this).attr("src"); // grab the src "attribute"
console.log(src);
console.log(src.indexOf("/") == 0); // true -> starts with /
console.log(src.indexOf("http://") == 0); // true -> starts with http://
console.log(src.indexOf("https://") == 0); // true -> starts with https://
});