天天看点

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

<span style="font-size:24px;">return '<button class="bt_row" data-toggle="modal" οnclick="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" οnclick="resi('+c.residentName+')">预约</button>';</span>
           

其中

<span style="font-size:24px;">οnclick="resi('+c.residentName+')"</span>
           

中 '+c.residentName+' 已经是一个字符串了,而就会变成resi(+c.residentName+),其中+c.residentName+ 已经被当成参数,此时+c.residentName+并没有转义和解析。

所以:当写成

<span style="font-size:24px;">οnclick="resi(\''+c.residentName+'\')"</span>
           

的时候,浏览器可以解析为'+c.residentName+'是一个变量,根据后台不同的值,从而解析为所选择的字符串或者其他的。

但是如果是number的话,

<span style="font-size:24px;"> οnclick="resi('+c.residentNO+')"</span>
           

这样就可以的,因为数字不用解析,可以直接被读到。

继续阅读