如何将变量添加到<img/>标记中?

时间:2016-11-08 02:05:19

标签: javascript html image

我想问一下如何将电子邮件ID传递给图片标签?

<li class="form-line" data-type="control_email" id="id_3">
<label class="form-label form-label-left form-label-auto" id="label_3" for="input_3"> E-mail: </label>
<div id="cid_3" class="form-input jf-required">
<input type="email" class=" form-textbox validate[Email]" id="input_3" name="q3_email" size="30" value="" />
</div>
</li>
<img src="https://api.qrserver.com/v1/create-qr-code/?    data=HelloWorld&amp;size=100x100" alt="" title="" />

1 个答案:

答案 0 :(得分:1)

从技术上讲,您可以将任何自己的自定义属性添加到HTML标记,然后通过javascript访问该信息。例如:

&#13;
&#13;
var x = document.getElementsByTagName("img")[0].getAttribute("imgSize"); 
alert(x);
&#13;
<img data="helloWorld" imgSize="100x100" src="https://api.qrserver.com/v1/create-qr-code/?    data=HelloWorld&amp;size=100x100"/>
&#13;
&#13;
&#13;

如果你的意思是动态地在图像源上添加值,而不是在javascript中使用类似的东西:

&#13;
&#13;
var imgUrl = "https://api.qrserver.com/v1/create-qr-code/";
//add data
imgUrl += "?data=HelloWorld";
//add size
imgUrl += "&amp;size=100x100";
// set image
document.getElementById("myImg").src=imgUrl;
&#13;
<img id="myImg" src="#" >
&#13;
&#13;
&#13;