Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
试试下面的。
var string = $('textarea').val(); var encoded = $('<div/>').text(string).html();
演示
它在这里打破
这不是对错误的非常有用的描述,但我猜你的意思是我得到一个空对象。
拼写textarea正确(中间没有一个e),以便您分配 textarea 的值而不是undefined(这是您在调用val()空 jQuery 对象时得到的)。
textarea
e
undefined
val()
这与 HTML 编码无关。HTML 根本不参与这个过程。您从 DOM 获取用户输入,然后将其放入 JSON。JSON.stringify 照顾您所有的编码需求。
Am trying to play YouTube and other videos by embedding it in the HTML source for displaying them in iOS.
Both <embed> and <object>
<embed>
<object>