jquery - .html() 不对传递的值进行编码

标签 jquery html encoding

我需要保留传递给 .html() 的每个字符按原样,即未编码。例如,.html('>')将小于号保存为 > ,不是&lt; 。我该怎么做?理想情况下,该解决方案将应用于所有“特殊”字符,例如< , &

<h1 id="my-title"></h1>

// JavaScript / jQuery code.
$('#my-title').html('>');
...
...
// Somewhere else in the code, I need to retrieve the value back by calling
// var v = $('#my-title').html();

最佳答案

使用text()代替html()

var v = $('#my-title').text();

关于jquery - .html() 不对传递的值进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9350441/

相关文章:

javascript - "prepend"CSS 转换

javascript - Backbone Marionette 目标元素在 el 与 jQuery

python-3.x - 为什么我得到 "UnicodeEncodeError: ' charmap' 编解码器无法在位置 84811 : character maps to <undefined>"error? 编码字符 '\u25b2'

database - 在 ISO-8859-1 Oracle 数据库中编码 "€"字符

css - Firefox/Safari 无法识别 Cloud.Typography 网络字体字符

javascript - jQuery 和 Javascript - 通过对象的属性缓慢循环

javascript - 使用 replaceWith() 替换数组中的逗号?

php - 如何制作带有小图像的下拉菜单?

html - 仅 CSS slider 按钮悬停问题 - 从 AMKohn 的 CodePen 修改

html - 创建 3 个完全相等的图片,占据页面的高度和宽度