You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Popover中添加图片作为属性值?(已解决)

Fix: Bootstrap Popover Displays Raw HTML Instead of Image

I ran into this exact issue where my Popover was showing the <img> tag as plain text instead of rendering the actual image. Here's the code I initially used to create the element and set up the Popover attributes:

var item = document.createElement('div'); 
var att2 = document.createAttribute("data-toggle"); 
var att3 = document.createAttribute("title"); 
var att4 = document.createAttribute("data-content"); 
var att5 = document.createAttribute("data-placement"); 

att4.value = "<img src='blablabla.png' />"; 
att3.value = itemname; 
att2.value = "popover"; 
att5.value = "top"; 

item.setAttributeNode(att5); 
item.setAttributeNode(att2); 
item.setAttributeNode(att3); 
item.setAttributeNode(att4);

The problem here is that Bootstrap Popover escapes HTML content by default—it treats whatever you put in data-content as plain text for security reasons, which is why your image tag was showing up as code instead of a rendered image.

To fix this, you just need to explicitly tell the Popover to render HTML content by adding html: true when initializing it. Here's the corrected initialization code:

$('[data-toggle="popover"]').popover({
  html: true,
  trigger: "hover",
  container: 'body'
});

Once you add that html: true option, the Popover will properly parse and render the image tag. Just make sure your image path (blablabla.png) is correct so the image loads as expected!

内容的提问来源于stack exchange,提问作者onurtore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:13:43