简体   繁体   English

在Javascript中获取标记的字符串属性

[英]Get string attribute of a tag in Javascript

I need some help to extract the inline image source (in this case #3) from the string below in javascript, then encapsulate and send it to php: 我需要一些帮助来从javascript中的下面的字符串中提取内联图像源(在本例中为#3),然后将其封装并发送到php:

<img width="100" id="1" style="display: none;" src="http://col.stb00.s-msn.com/i/4E/5EA45CFEC5FF5726D86E65CEE815D.jpg">
<img width="100" id="2" style="display: none;" src="http://col.stb01.s-msn.com/i/36/59F78F98816E925C8A18FBCF013D5.jpg">
<img width="100" id="3" style="display: inline;" src="http://col.stb00.s-msn.com/i/6F/D11A5421FDC5E8C5CEA4D19BCC7A5.jpg">
<img width="100" id="4" style="display: none;" src="http://col.stb00.s-msn.com/i/88/B51A1462A325FF345AC442688F7A8.jpg">
<img width="100" id="5" style="display: none;" src="http://col.stb01.s-msn.com/i/39/8A811756CB49259F65032AB9F1D78.jpg">

Is there an easy way to do it please? 请问有一个简单的方法吗? thanks 谢谢

var imgs = document.getElementsByTagName('img');

for (var i=0; i<imgs.length; i++) {
    if (imgs[i]["style"]["display"] === "inline") {
        console.log(imgs[i]["src"]);
    }
}

Easy. 简单。 Use DOM: 使用DOM:

function getSrc(id)
{
    var src = document.getElementById(id).src;
}

The variable src will hold the value of the src attribute in your tag. 变量src将保存标记中src属性的值。 Do this for all of the tags that need their src elements extracted. 对需要提取src元素的所有标记执行此操作。 You will probably need to use AJAX to pass this to PHP. 您可能需要使用AJAX将其传递给PHP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM