gpt4 book ai didi

javascript - 如何使用 javascript 转义 "innerHtml"属性中的 html?

转载 作者:行者123 更新时间:2023-11-29 22:30:42 27 4
gpt4 key购买 nike

我有一个 javascript 和我的 jsp 的组合,它正在添加一些文本并将其转义。我还有一些 javascrpt 可以让我在提交后编辑文本。但是,当单击编辑按钮时,html 会呈现。

    function editCommentToggle( id )
{
theRow = document.getElementById("id"+id);
//user = theRow.cells[0].innerHTML;
//date = theRow.cells[1].innerHTML;
--> com = theRow.cells[2].innerHTML;


idx = 2;
maxlength = 250;

// Comment field
cell = theRow.cells[idx];
while( cell.childNodes.length > 0 ) cell.removeChild(cell.childNodes[0]);
element = document.createElement("textarea");
element.id="comments-"+id;
element.rows="3";
element.value = com;
element.style.width = "400px";
element.maxLength = "250";
element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounterEdit(undefined, maxlength, this);};
cell.appendChild(element);

“theRow.cells[2].innerHTML;” 正在抓取该单元格中的文本或 html,但如果说有一个“换行符”,它会显示一个 <br> .....我应该如何构造它以保留转义的 html??

谢谢

最佳答案

使用 textContent(或 IE 中的 innerText)

com = theRow.cells[2].textContent || theRow.cells[2].innerText

关于javascript - 如何使用 javascript 转义 "innerHtml"属性中的 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7069200/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com