[英]image src from a var?
I have a var that contains a number and I want to store that in image src like: 我有一个包含数字的var,我想将它存储在图像src中:
var number = 10;
<img src="images\'+number+'.jpg">
i already have a picture with called 10.jpg 我已经有一张叫做10.jpg的照片
also how can I change a string to a number. 还如何将字符串更改为数字。 for example if I use reg-exp to get a number from text how can I change it to a number ? 例如,如果我使用reg-exp从文本中获取数字,我该如何将其更改为数字?
To set the image source you can do 要设置图像源,您可以执行此操作
var image = document.getElementById("myImage");
image.src = number + ".jpg";
You can change a string into a number using the parseInt function. 您可以使用parseInt函数将字符串更改为数字。
parseInt("10");
or 要么
parseInt("10", 10);
where the second parameter is the radix representing the number system. 其中第二个参数是表示数字系统的基数。
如果你使用jQuery
var number = $("#imageid").attr('src').replace(".jpg", "");
For the sake of time, here's the jQuery solution 为了时间,这里是jQuery解决方案
var myVar = "10";
$("#myImage").attr("src",myVar+".jpg");
to convert a string to base 10 integer, @Garett's answer is usable 要将字符串转换为基数为10的整数,@ Garett的答案是可用的
var myVarAsInt = parseInt("10", 10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.