javascript - 将 html 添加到字符串 svelte
问题描述
嘿,我有几串文字
let text = [
"<strong>Voiijer</strong>" +
"is a global community of explorers of all ages who create and share expeditions",
"Everyone is an explorer, now everyone can explore. Voiijer is for everyone, from students to scientists.",
"Voiijer is for the naturally curious. <br />To tell the story of the world.",
];
我这样展示
<p>{text[index]}</p>
索引是 carousal 元素的索引,但 html 元素仅显示为文本。有人知道如何解决这个问题吗?
解决方案
你可以用{@html text[index]}
但是,根据文档:
在将 {@html ...} 内的表达式插入 DOM 之前,Svelte 不会对其进行任何清理。换句话说,如果您使用此功能,您必须手动转义来自您不信任的来源的 HTML,否则您可能会使您的用户面临 XSS 攻击。
您应该确保自己的字符串是安全的。
推荐阅读
- nginx - 有时 502 Bad Gateway ,有时 504 网关超时,有时网站加载成功
- docker - 如何在非 SUSE 主机操作系统上的 SUSE 容器中运行 zypper 命令(如果可能的话?)
- react-native - 如何在启用滑动的情况下使用 createMaterialTopTabNavigator 时使用水平滚动视图,REACT NATIVE
- java - 这个 Jakarta Mail 代码究竟加密了什么,为什么收到的电子邮件被标记为未验证?
- c++ - 了解新的编码技术
- c# - Double Relation One to Many (different Property same Class), Unable to determine the relationship represented by navigation, C#
- python - 使用 pyspark 从 JSON DF 数组中删除选择性 JSON
- python - 如何过滤来自 django 网站的草稿内容?
- bash - for循环不同的%变量(跨不同的操作系统)
- angular - 前端的身份