2

我正在寻找从图像标签显示图像域名的方法

<p>aabb .</p><br><center>
<img itemprop="image" src="http://subww.somedomain.com.uk/images/news/Image/2013/09/20/6523c3850ef881.img.jpg" alt="bbddd ddd" class="dddd" border="0" height="225" width="300"><br><i>lkjlkkjf</i></center>
<img src="http://www.somebbbdomain.com/images/news/Image/2013/09/20/6523c3850ef881.img.jpg" alt="bbddd ddd">
<div style="clear: both;"></div><p>aabbcc</p>

javascript 将查找图像的第一个 src,然后提取警报域名..“somedomain.com.uk”到屏幕。

4

1 回答 1

1

喜欢:

var imgEles = document.getElementsByTagName("img"),
    pattern = /^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i,
    domains = [];
for( var im = 0; im < imgEles.length; im++) {
   var src = imgEles[im].src;
   var matches = src.match(pattern);
   var domain = matches && matches[1]; 
   domains.push( domain );
}
console.log( domains );
于 2013-09-23T05:52:06.553 回答