从没有jQuery的字符串中获取图像src
get Image src from string without jQuery
我有一个字符串(不在我的文档中),比如
<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/bear-extender- samsungativ_thumbnail.jpg"> Freedom is a wonderful thing and the folks at BearExtender want Microsoft users to enjoy more of it. Its new 1,200mW USB WiFi booster for PCs finally caught up with the Mac version, which extends WiFi range up to four times more than usual. One lucky reader will get to savor this new found freedom ...<div>
我想剥离字符串中第一个图像的来源。我知道我可以通过以下方式使用 jquery 来做到这一点: String.find('img').first().attr('src');
没有jquery,我怎么能实现同样的事情?
好吧,
您想对其进行一些简单的JS测试,以从那里剥离src,如下所示:
var str = '<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/bear-extender- samsungativ_thumbnail.jpg"> Freedom is a wonderful thing and the folks at BearExtender want Microsoft users to enjoy more of it. Its new 1,200mW USB WiFi booster for PCs finally caught up with the Mac version, which extends WiFi range up to four times more than usual. One lucky reader will get to savor this new found freedom ...<div>'
var imgExists = str.indexOf('<img src="');
if (imgExists > -1) {
var i = imgExists + 10;
str = str.substr(i);
str = str.substr(0, str.indexOf('"'));
alert(str);
// returns = http://www.blogcdn.com/www.engadget.com/media/2013/10/bear-extender- samsungativ_thumbnail.jpg
// to remove all the spaces you might also want to do another replace after the last 2 substr's
// str = str.replace(/'s/g, '');
}
js小提琴演示
你可以通过以下方式获得它:
var i = '<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/bear-extender- samsungativ_thumbnail.jpg"> Freedom is a wonderful thing and the folks at BearExtender want Microsoft users to enjoy more of it. Its new 1,200mW USB WiFi booster for PCs finally caught up with the Mac version, which extends WiFi range up to four times more than usual. One lucky reader will get to savor this new found freedom ...<div>';
alert(i.substring(i.indexOf('http'), i.lastIndexOf('"')));
我可以看到您在div
中使用了这个img
。将div
中的id
参数设置为 id="imgDiv"
。然后你可以用这个语法来解决问题:
<script>
var div = document.getElementById("imgDiv");
var image = div.getElementsByTagName("img")[0];
alert(image.src);
</script>
相关文章:
- 点击(右键点击)使用传单地图库获取图像覆盖的像素坐标
- 如何使用javascript和xpath获取图像src
- 如何使用javascript从文件夹中获取图像并发布直接链接
- 如何从 javascript 中可能具有一个或多个图像的字符串中仅获取图像名称
- 获取图像节点js的主色
- 获取图像的平均外部像素颜色
- 使用asp.net FileUpload时从javascript获取图像宽度和高度
- jQuery:获取图像加载错误的详细信息
- 使用回调获取图像像素数据
- Javascript 正则表达式.获取图像源
- 获取图像宽度的最早事件
- 使用 JsonWebToken 授权标头获取图像
- 是从画布获取图像数据的任何方法,从远程视频中提取
- 铯:使用自己的OpenStreetMap服务器.:“无法获取图像磁贴”错误
- 从JSON数据中获取图像源
- 获取图像's磁盘中文件的实际高度和宽度
- 使用Jquery获取图像中心的文本
- 如何获取图像的宽度并将该值用作高度以使图像相当方正
- 在angular js和ionic中无法在服务器上捕获和上传图像以及从服务器上获取图像
- PhoneGap+JQuery_Mobile:如何通过单击按钮从照片/图像库中获取图像