Javascript how to escape characters

后端 未结 1 1617
执笔经年
执笔经年 2021-01-21 16:33

I want to insert some html into a div suppose this:

I am using $(\'#Sag\').html(data), in ord

相关标签:
1条回答
  • 2021-01-21 16:50

    so how can I escape ",',... should I use a \ before each one, or is there something like @ in c# to use in javascript

    No, JavaScript doesn't have an equivalent of C#'s @ feature.

    In JavaScript, strings can be quoted by either single (') or double (") quotes. Within a quote, only the style you've used for quoting needs to be escaped. So within a string quoted with single quotes, double quotes don't need escaping; and within a string using double quotes, single quotes don't need escaping. (It's always okay to escape them, all that varies is whether you have to.)

    So you usually pick the one you're using least, and use that for the main string's delimiter. E.g.:

    str = "This uses doubles, so I don't have to worry about the ' in \"don't\".";
    

    Note that I didn't have to escape ' there, but I did have to escape ".

    Similarly:

    str = 'This uses singles, so I don\'t have to worry about "quoting" things.';
    

    There I had to escape ' but not ".

    0 讨论(0)
提交回复
热议问题