admin管理员组

文章数量:1332377

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?

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?

Share Improve this question edited Feb 19, 2012 at 14:37 James M 16.7k3 gold badges49 silver badges57 bronze badges asked Feb 19, 2012 at 14:35 EhsanEhsan 4,4747 gold badges41 silver badges59 bronze badges 3
  • 1 Your data is what? How are you getting that chunk of code? – epascarello Commented Feb 19, 2012 at 14:39
  • What do you intend to happen when you it in the div. Note that the php will not execute as javascript is clientside. – Ash Burlaczenko Commented Feb 19, 2012 at 14:40
  • the data is something that is using the session in order to show the user's cart, so is there any other idea for this purpose. P.S: the content of the specified div would be shown to the user as a pop up window so I think it's my solution to show the user this content without reloading the page – Ehsan Commented Feb 19, 2012 at 14:49
Add a ment  | 

1 Answer 1

Reset to default 8

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

本文标签: Javascript how to escape charactersStack Overflow