javascript - InsertAfter 使用 Javascript 的长 HTML 块
问题描述
我试图在使用 javascript 的 div 元素之后插入一个长的 HTML 块。我已经尝试了几种方法,但似乎都没有奏效。这就是我现在所拥有的。如何在使用 Javascript 的 div 容器之后插入一长段 HTML?
注入 Javascript
<script>
$(function() {
$( "<p class="" style="white-space:pre-wrap;" id="yui_3_17_2_1_1582132982979_54">Sabine the Queen<br>
<strong data-shrink-original-size="75">A Global Real Estate & Lifestyle Brand<br></strong>
<a href="/properties" target="">Our Properties</a></p>" ).insertAfter( ".sqs-gallery" );
});
</script>
容器
<div class="sqs-gallery">
This is a slideshow.
</div>
解决方案
看起来您有未转义的引号,可能会弄乱 HTML 标记。将外引号换成单引号或转义内引号
交换引号:
<script>
$(function() {
$( '<p class="" style="white-space:pre-wrap;" id="yui_3_17_2_1_1582132982979_54">Sabine the Queen<br>
<strong data-shrink-original-size="75">A Global Real Estate & Lifestyle Brand<br></strong>
<a href="/properties" target="">Our Properties</a></p>' ).insertAfter( ".sqs-gallery" );
});
</script>
请注意我在 HTML 字符串的开头和结尾处添加的单引号
逃脱:
<script>
$(function() {
$( "<p class=\"\" style=\"white-space:pre-wrap;\" id=\"yui_3_17_2_1_1582132982979_54\">Sabine the Queen<br>
<strong data-shrink-original-size=\"75\">A Global Real Estate & Lifestyle Brand<br></strong>
<a href=\"/properties\" target=\"\">Our Properties</a></p>" ).insertAfter( ".sqs-gallery" );
});
</script>
要转义字符串中的双引号,应在其前面加上反斜杠。这告诉浏览器这个引号不是第一个的伙伴并继续前进。如果它未转义,那么它认为字符串比你的意思短很多并且会中断
推荐阅读
- vhdl - 正确编译的代码的 ModelSim Altera 仿真错误:处理中的致命错误
- jquery - 循环似乎结束了,即使期望是重置
- react-native - 如何从数组中制作卡片?
- python - 想要在 Python 中显示拟合方程
- vb.net - 如何消除程序集的冗余副本
- python - 如何从 GoogleCredentials 获取 projectId?
- c# - 玩家接触地面标记地面时不会死亡
- html - 如何解析 Google 地图嵌入代码以便更改缩放?
- javascript - 如何从非本地 Web 服务器控制 Raspberry Pi?
- java - 从Java中的多个双数(不是数组)中查找最大值?