Jquery单引号和双引号的使用注意

据Jquery文档显示在js中单引号和双引号都是一样的,但是在实际使用就碰到了问题,如下面的例子,在嵌套使用时,如果都使用双引号,内层的双引号的无效的

<script src="../../scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
	$("#send").click(function() {//外层对象使用双引号
		$.ajax({
			  type: "GET",
			  url: "test.json",
			  dataType: "json",
			  success:function(data){
				  //$("#resText").empty();//内层对象使用双引号,无效
				  $('#resText').empty();//内层对象使用单引号,有效
				  var html='';
				  $.each(data,function(commentIndex, comment) {
					  //同理,内层html,使用单引号,但是它的下一层,要使用双引号,才生效
					  html += '<div class="comment"><h6>' + comment['username'] + ':</h6><p class="para">' + comment['content'] + '</p></div>';
				  });
				  //$("resText").html(html);
				  $('#resText').html(html);
				  
			  }
			});
	});
})

通过上面的例子,可以总结一下就是, 平时使用的时候尽量用单引号,只有碰到嵌套的时候才会同时用两种引号。即外面是单引号的时候里面就要用双引号,外面是双引号的时候里面就要用单引号,总之不能同时用双引号或者是单引号

猜你喜欢

转载自blog.csdn.net/xufengzhu/article/details/73231901