您的位置:首页 > Web前端 > JavaScript

onclick中字符串转义,函数调用的问题

2016-10-19 10:44 183 查看
<span style="font-size:24px;">return '<button class="bt_row" data-toggle="modal" onclick="resi(\''+c.residentName+'\')">预约</button>';</span>
<span style="font-size:24px;"><span style="font-family: SimSun; background-color: rgb(255, 255, 255);">碰到的问题:</span></span>
  想要实现的结果:点击button,调用函数resi,并将参数传给此函数。但是参数并不是一个固定的,而是从后台读出来的,必须进行解析。
 当写成这样的时候: 
<span style="font-size:24px;">return '<button  class="bt_row" data-toggle="modal" onclick="resi('+c.residentName+')">预约</button>';</span>
其中
<span style="font-size:24px;">onclick="resi('+c.residentName+')"</span>


中 '+c.residentName+' 已经是一个字符串了,而就会变成resi(+c.residentName+),其中+c.residentName+ 已经被当成参数,此时+c.residentName+并没有转义和解析。
所以:当写成
<span style="font-size:24px;">onclick="resi(\''+c.residentName+'\')"</span>
的时候,浏览器可以解析为'+c.residentName+'是一个变量,根据后台不同的值,从而解析为所选择的字符串或者其他的。
但是如果是number的话,
<span style="font-size:24px;"> onclick="resi('+c.residentNO+')"</span>


这样就可以的,因为数字不用解析,可以直接被读到。
   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息