[英]How to display images to html file using jquery? (some php and xml)
I have a folder with images called " LaptopsImages ".我有一个名为“ LaptopsImages ”的图像文件夹。
In my file called laptops.php i have an array list with different laptops with variables like: title, brand, price, description, spec and pictures names.在我的名为 notebooks.php 的文件中,我有一个包含不同笔记本电脑的数组列表,其中包含以下变量:标题、品牌、价格、描述、规格和图片名称。 My array list is convert to XML DOM tree.我的数组列表转换为 XML DOM 树。
On my file called product.html i wrote a code that take every product data into the variables and display it:在名为 product.html 的文件中,我编写了一个代码,将每个产品数据放入变量中并显示出来:
var title = $theProduct.find('title').text();
var brand = $theProduct.find('brand').text();
var price = $theProduct.find('price').text();
var description = $theProduct.find('description').text();
var spec = $theProduct.find('spec');
var imageFilename = $theProduct.find('picture').text();
document.getElementById("title").innerHTML=title
document.getElementById("brand").innerHTML=brand
document.getElementById("price").innerHTML=price
document.getElementById("description").innerHTML=description
document.getElementById("spec").innerHTML=description
I have accomplished to display every product data into my html except the pictures for each laptop.我已经完成了将每个产品数据显示到我的 html 中,除了每台笔记本电脑的图片。 Is any way that i can display pictures with the name they have already from my variable?有什么办法可以用我的变量中已有的名称显示图片? Assume that the pictures have names " 0001.jpg , 0002.jpg " and so on.假设图片的名称为“ 0001.jpg , 0002.jpg ”等。
Every picture can be taken from: var imageFilename=$theProduct.find('picture').text();每张图片都可以从: var imageFilename=$theProduct.find('picture').text();
document.getElementById("picture").src = imageFilename
如果有带有 id picture
的<img>
标签
如果您使用 jquery,您可以执行以下操作:
$("#picture").attr("src", imageFilename);
document.getElementById('myImage').src = imageFileName;
If you need the name from the ImageFileName then you can use:如果您需要 ImageFileName 中的名称,则可以使用:
name= imageFileName.substring(0, imageFileName.length - 4);/*to chop off .JPG*/
With Jquery:使用jQuery:
$("#myImage").attr("src", imageFilename);
document.getElementById("price").innerHTML= "<img src=\"" + imageurl+ "\">";
您可以使用此代码在内部 div 中显示完整图像,其中 div id 是价格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.