简体   繁体   English

在中使用var <img src> 标签

[英]Using a var in an <img src> tag

I've been trying to work out my own photo album when I encountered an error. 遇到错误时,我一直在尝试制作自己的相册。 It's a syntax error but after trying out a number of different ways of writing the code it just wont work. 这是一个语法错误,但是在尝试了多种不同的编写代码方式后,它便无法正常工作。

Here's what I did. 这就是我所做的。 I made a var image which contains the string file path (using string manipulation) of the image (I am not to use PHP just yet, so it can only accept photos from a given folder named "images" on the base directory) and another var imgSource to contain the actual image itself using the var image . 我制作了一个var图像 ,其中包含图像字符串文件路径(使用字符串操作)(我现在还不使用PHP,因此它只能接受基目录中名为“ images”的给定文件夹中的照片) var imgSource使用var image包含实际图像本身。

The code is: 代码是:

var image = "images/" + $(".inputImage").val().split('\\').pop();
var imgSource = $('<img src = image alt = "Image not found" class = "card-img-top">');

for some reason, even without the apostrophes, the tag reads the word image as the filepath (string) not as variable I've created. 由于某种原因,即使没有撇号,标签也会将单词image读取为文件路径(字符串),而不是我创建的变量

你会用

var imgSource = $('<img src="' + image + '" alt="Image not found" class="card-img-top">');
var image = "images/" + $(".inputImage").val().split('\\').pop();
var imgSource = $('<img src="' + image + '" alt="Image not found" class="card-img-top">');

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

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