Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从不带jQuery的字符串中获取图像src_Javascript_Html - Fatal编程技术网

Javascript 从不带jQuery的字符串中获取图像src

Javascript 从不带jQuery的字符串中获取图像src,javascript,html,Javascript,Html,我有一个字符串(不在我的文档中),如 自由是一件美妙的事情,BearExtender的员工希望微软用户能享受到更多。其新推出的1200MW PC USB WiFi扩展器终于赶上了Mac版本,其WiFi范围比平时扩大了四倍。一个幸运的读者将品尝到这种新发现的自由。。。 我想去掉字符串中第一个图像的源。我知道我可以使用jquery,通过使用: String.find('img').first().attr('src') 没有jquery我如何实现同样的目标?您可以通过以下方式实现: var i =

我有一个字符串(不在我的文档中),如

自由是一件美妙的事情,BearExtender的员工希望微软用户能享受到更多。其新推出的1200MW PC USB WiFi扩展器终于赶上了Mac版本,其WiFi范围比平时扩大了四倍。一个幸运的读者将品尝到这种新发现的自由。。。
我想去掉字符串中第一个图像的源。我知道我可以使用jquery,通过使用:
String.find('img').first().attr('src')

没有jquery我如何实现同样的目标?

您可以通过以下方式实现:

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('"')));
var i=”自由是一件美妙的事情,BearExtender的员工希望微软用户能享受更多自由。其新推出的1200MW PC USB WiFi扩展器终于赶上了Mac版本,其WiFi范围比平时扩大了四倍。一个幸运的读者将品尝到这种新发现的自由;
警报(i.substring(i.indexOf('http')、i.lastIndexOf('“')));
您可以通过以下方式获得:

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('"')));
var i=“自由是一件美妙的事情,BearExtender的员工希望微软的用户能享受到更多。它为个人电脑提供的新的1200MW USB WiFi扩展器终于赶上了Mac版本,它将WiFi的范围扩展到平常的四倍。一个幸运的读者将享受到这种新发现的自由……”;
警报(i.substring(i.indexOf('http')、i.lastIndexOf('“')));

您想对它进行一些简单的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, ''); 
}
var str=”自由是一件美妙的事情,BearExtender的员工希望微软的用户能享受更多自由。其新推出的1200MW PC USB WiFi扩展器终于赶上了Mac版本,其WiFi范围比平时扩大了四倍。一个幸运的读者将品尝到这种新发现的自由
var imgExists=str.indexOf('-1){
var i=imgExists+10;
str=str.substr(i);
str=str.substr(0,str.indexOf(“”);
警报(str);
//返回=http://www.blogcdn.com/www.engadget.com/media/2013/10/bear-extender-    samsungativ_缩略图.jpg
//要删除所有空格,您可能还需要在最后2个子字符串之后进行另一次替换
//str=str.replace(/\s/g');
}

您希望对其进行一些简单的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, ''); 
}
var str=“自由是一件美妙的事情,BearExtender的员工希望微软的用户能享受到更多。它为个人电脑提供的新的1200MW USB WiFi扩展器终于赶上了Mac版本,它将WiFi范围扩展到平常的四倍。一个幸运的读者将享受到这种新发现的自由……”
var imgExists=str.indexOf('-1){
var i=imgExists+10;
str=str.substr(i);
str=str.substr(0,str.indexOf(“”);
警报(str);
//返回=http://www.blogcdn.com/www.engadget.com/media/2013/10/bear-extender-    samsungativ_缩略图.jpg
//要删除所有空格,您可能还需要在最后2个子字符串之后进行另一次替换
//str=str.replace(/\s/g');
}

我可以看到您正在
div
中使用此
img
。将
div
中的
id
参数设置为
id=“imgDiv”
。然后您可以使用此语法来解决此问题:

<script>
  var div = document.getElementById("imgDiv");
  var image = div.getElementsByTagName("img")[0];
  alert(image.src);
</script>

var div=document.getElementById(“imgDiv”);
var image=div.getElementsByTagName(“img”)[0];
警报(image.src);

我可以看到您正在
div
中使用此
img
。将
div
中的
id
参数设置为
id=“imgDiv”
。然后您可以使用此语法来解决此问题:

<script>
  var div = document.getElementById("imgDiv");
  var image = div.getElementsByTagName("img")[0];
  alert(image.src);
</script>

var div=document.getElementById(“imgDiv”);
var image=div.getElementsByTagName(“img”)[0];
警报(image.src);

如果不想使用jQuery,为什么要用jQuery标记问题?如果不想使用jQuery,为什么要用jQuery标记问题?同样,我的HTML代码中没有。正如我所说,它是一个字符串。同样,我的HTML代码中没有它。正如我所说,这是一根绳子。