How do I escape a string inside JavaScript code inside an onClick handler?

前端 未结 13 903
半阙折子戏
半阙折子戏 2020-11-28 03:44

Maybe I\'m just thinking about this too hard, but I\'m having a problem figuring out what escaping to use on a string in some JavaScript code inside a link\'s onClick handle

13条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 04:06

    I have faced this problem as well. I made a script to convert single quotes into escaped double quotes that won't break the HTML.

    function noQuote(text)
    {
        var newtext = "";
        for (var i = 0; i < text.length; i++) {
            if (text[i] == "'") {
                newtext += "\"";
            }
            else {
                newtext += text[i];
            }
        }
        return newtext;
    }
    

提交回复
热议问题