我需要在 div 中顯示用戶輸入。但是,如果用戶輸入標簽,它將執行腳本標簽內的任何內容。例如<html><body> <button onclick="runTest('<script>alert(\'test\')</script>')">Click Me</button> <div id="test"></div></body></html>還有我的 javascript runTest 函數function runTest(str) { $('#test').append($('<div>' + str + '</div>'));}如果我運行它,它將導致執行 alert()。我嘗試使用 escape(str),但它顯示轉義字符%3Cscript%3Ealert%28%27test%27%29%3C/script%3E任何想法?這是提琴手:https://jsfiddle.net/zvLg1w6q/1/
在 div 中顯示 <script> 標簽
青春有我
2023-05-19 19:52:18