gpt4 book ai didi

javascript - 如何将长值传递给javascript函数

转载 作者:行者123 更新时间:2023-11-28 14:03:04 24 4
gpt4 key购买 nike

我有 JavaScript 函数:

function someAction(thisTd,text){
alert(text);
thisTd.innerHTML=text;
...
}

和 html 文件:

<td onclick="someAction(this,<?echo 'Long-long text with <b>html-formatting</b>'?>)"/>

当我使用这样的代码函数 someAction 不会调用(因为警报没有显示)并且在 Opera 的错误控制台中没有显示错误。如何解决这个问题?
附:我不使用框架(JQuery 等)。
更新#1
当我使用这样的代码时:

<?$encoded=str_replace("\n","",str_replace("\r\n","",$text));echo $encoded?>

效果很好。但我不确定它在 Linux 中是否正常工作。(我使用 Windows)

最佳答案

确保您HTML encode它并在参数两边加上单引号:

<td onclick="someAction(this, '<?echo htmlspecialchars('Long-long text with <b>html-formatting</b>', ENT_QUOTES) ?>')"/>

关于javascript - 如何将长值传递给javascript函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3984778/

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