简体   繁体   English

Javascript如何转义字符

[英]Javascript how to escape characters

I want to insert some html into a div suppose this: 我想在div中插入一些html:

<div id="Sag">
 </div>

I am using $('#Sag').html(data) , in order to insert data into this div... but here is the problem my data is: 我正在使用$('#Sag').html(data) ,以便将数据插入到此div中……但这是我的数据存在的问题:

<table style="direction: rtl;float:right;">
                    <?php $sess = isset(Yii::app()->session['cart']) ? Yii::app()->session['cart'] : '{}';
                        $ses = json_decode($sess, true);
                        foreach ($ses as $i=>$value11){
                        ?>
                <tr style="direction: rtl;" class="cart_show">
                     <td>
                         <img class="picCart-<?php echo $i; ?>" src="<?php echo Yii::app()->request->baseUrl."/images/".$ses[$i]['properties']['pic_directory'];?>" width="100" heigh="100">
                     </td>
                    <td class="descCart-<?php echo $i; ?>">
                        <?php echo $ses[$i]['properties']['description'];?>
                    </td>
                    <td class="priceCart-<?php echo $i; ?>">
                        <?php echo $ses[$i]['properties']['price'];?>
                    </td>
                    <td class="quantityCart-<?php echo $i; ?>">
                        <input type="text" style="width: 20px;" class="voroodi" value="<?php  
                        echo $ses[$i]['quantity'];
                        ?>">
                        <button name="delete_from_cart-<?php echo $i; ?>" class="btnDel">??? </button>
                        <button name="modify_cart-<?php echo $i; ?>" class="btnModify">?????</button>
                    </td>
                    </tr>
                    <?php } ?>
                        </table>    

so how can I escape " , ' , ...? should I use a \\ before each one, or is there something like @ in C# to use in javascript? 所以我该如何转义"' ,...? " ,我应该在每个\\之前使用\\ ,还是在JavaScript中在C#中使用类似@东西?

so how can I escape ",',... should I use a \\ before each one, or is there something like @ in c# to use in javascript 所以我该如何转义“,',...我应该在每个字符之前使用\\,还是在JavaScript中在c#中使用@

No, JavaScript doesn't have an equivalent of C#'s @ feature. 不,JavaScript没有C#的@功能。

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.) 在JavaScript中,字符串可以用单引号( ' )或双引号( " )引起来。在引号内,只需要转义用于引用的样式。因此,在用单引号引起来的字符串中,双引号不要不需要转义;在使用双引号的字符串中,单引号不需要转义(逃脱它们总是可以的 ,所有不同之处在于是否必须这样做)。

So you usually pick the one you're using least, and use that for the main string's delimiter. 因此,通常选择最不常用的那个,并将其用作主字符串的定界符。 Eg: 例如:

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 " . 在那儿我必须逃脱'而不是"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM