简体   繁体   中英

Javascript how to escape characters

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

<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:

<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?

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. 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 " .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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