如何在JavaScript对象中添加带有段落的对象键值中的图像

ryoqjall  于 2022-12-02  发布在  Java
关注(0)|答案(1)|浏览(100)

基本上,这个网页有两种语言的内容,这就是为什么我想从JavaScript对象传递带有段落的图像。但是它显示的是[对象HTMLImageElement]而不是图像。
HTML格式:

<p id="ourvisioncardparagraph" >
   // Here the image will appear with paragraph tag
  </p>

js:

var img = new Image();
img.src ="https://images.unsplash.com/photo-1664575198308-3959904fa430?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80";
img.alt = "star"

var data = {
  english: {
    ourvisioncardparagraph: `To reinvent finance through innovation & technology ${img} making 
      it convenient, accessible & fair to all.`
}
}
ou6hu8tu

ou6hu8tu1#

你可以使用img.outerHTML来得到一个元素html.所以总而言之:

var img = new Image();
img.src ="https://images.unsplash.com/photo-1664575198308-3959904fa430?ixlib=rb-4.0.3&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80";
img.alt = "star";

var data = {
  english: {
  ourvisioncardparagraph: `To reinvent finance through innovation & technology ${img.outerHTML} making  it convenient, accessible & fair to all.`
    }
}

document.getElementById("ourvisioncardparagraph").innerHTML = data.english.ourvisioncardparagraph;

相关问题