2012-02-19 81 views
2

我要插入一些HTML到一个div想这:的Javascript如何逃脱字符

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

我使用$('#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>  

所以我怎么能逃脱"',...?我应该在每一个之前使用一个\,还是有像在C#中使用JavaScript的@

+1

你的数据是什么?你如何得到这段代码? – epascarello 2012-02-19 14:39:56

+0

你打算在div中发生什么?请注意,php不会执行,因为JavaScript是客户端。 – 2012-02-19 14:40:45

+0

数据是使用会话来显示用户的购物车的东西,所以有什么其他的想法来达到这个目的。 P.S:指定div的内容将作为弹出窗口显示给用户,所以我认为这是我的解决方案,向用户显示此内容而不重新加载页面 – Ehsan 2012-02-19 14:49:21

回答

9

所以我怎能逃脱”,‘......我应该用一个\每一个之前,或者是有什么样@在C#在JavaScript中使用

不,没有按的JavaScript’吨有的C#的@功能等效的。

在JavaScript中,字符串可以通过单(')或双(")报价。在报价中,只有你使用的引用需要的样式进行转义被引用。所以在用单引号括起来的字符串中,双引号不需要转义;而在一个字符串中我们双引号,单引号不需要转义。 (它总是没关系逃避他们,所有的变化是你是否有来。)

所以你通常选择你正在使用至少一个,并使用该主字符串的分隔符。例如: -

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

请注意,我没有逃避',但我确实有逃避"

同理:

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

有我不得不逃离'但不"