在JQuery中添加带引号的html

在JQuery中添加带引号的html,jquery,Jquery,当用户在我的网站的feeds部分单击“like”时,我会添加一些HTML标记。但它正在崩溃。我在网上尝试了很多解决方案,但都找不到 以下是我正在使用的代码: $('#feed20').find('strong') .append('<span original-title="<ul><li> <a href="http://localhost/forex/profile/username">username</a> </li>&l

当用户在我的网站的feeds部分单击“like”时,我会添加一些HTML标记。但它正在崩溃。我在网上尝试了很多解决方案,但都找不到

以下是我正在使用的代码:

$('#feed20').find('strong')
.append('<span original-title="<ul><li>
<a href="http://localhost/forex/profile/username">username</a>
</li></ul>" custom-style="view-more" class="tiply_html_click">1 more</span>');
$('#feed20')。查找('strong'))
.append('1 more');
来自:

custom style=“查看更多”class=“tiply\u html\u单击“>1更多”)


是错误的…

您的报价有问题。使用双引号包装字符串,但随后在字符串中使用双引号,因此终止字符串文字

尝试像这样转义内部双引号(
\“
)-

'<span original-title="<ul><li><a href=\"http://localhost/forex/profile/username\">username</a></li></ul>" custom-style="view-more" class="tiply_html_click">1 more</span>'
'1更多'

HTML属性中的数据必须进行HTMLentized,请尝试:

$('#feed20').find('strong').append('<span original-title="&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://localhost/forex/profile/username&quot;&gt;username&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;" custom-style="view-more" class="tiply_html_click">1 more</span>');
$('feed20')。查找('strong')。追加('1 more');

您需要转义您的引号,请尝试以下操作

$(“#feed20”)。查找(“strong”)。追加(“1个以上”);

您需要转义
中使用的双引号“custom style=“view more”class=“tiply\u html\u click”>1 more”);

您应该对href使用转义单引号

$('#feed20').find('strong').append('<span original-title="<ul><li><a href=\'http://localhost/forex/profile/username\'>username</a></li></ul>" custom-style="view-more" class="tiply_html_click">1 more</span>');
$('feed20')。查找('strong')。追加('1 more');

您的HTML是什么,您的输入是什么,您希望输出是什么?而且在
span
中不能包含
ul
,这将是无效的HTML。@DavidThomas:这里我不想要任何有效的HTML。我需要将它们安全地插入到强标记中。您明确希望使用无效的HTML?因为这可能会,而且是不可预测的,使你的JavaScript有些混乱。@DavidThomas不,但这里的问题不是有效的html。但将其附加到内容中是一个问题,如何才能附加无效的html。。。无效的html被浏览器忽略或替换为其他内容。自定义样式也是一个属性。此处将进行解释。显然有问题,但帮助OP包括建议如何解决问题…@Udhay,我不是说自定义样式错误,只是指向标记中无效的部分。
$('#feed20').find('strong').append('<span original-title="<ul><li><a href=\'http://localhost/forex/profile/username\'>username</a></li></ul>" custom-style="view-more" class="tiply_html_click">1 more</span>');
$('#feed20').find('strong').append('<span original-title="<ul><li><a href=\'http://localhost/forex/profile/username\'>username</a></li></ul>" custom-style="view-more" class="tiply_html_click">1 more</span>');