简体   繁体   English

JavaScript函数中的单引号?

[英]Single quotes in javascript function?

I have created my table dynamically and in one of my td's I have function where I'm passing two different parameters. 我已经动态创建了表,并且在我的td之一中,我具有传递两个不同参数的函数。 One of them is ID and does not need quotes around but second parameter need single quotes and I tried so many combinations like &quote; 其中之一是ID,不需要引号,但第二个参数则需要单引号,我尝试了很多类似“ quote; or \\' and I could not get what I need. 或\\',我无法获得所需的东西。 Here is my code: 这是我的代码:

tbl +="<td><a onClick='DeleteRes(slotsPtc,"+jsData[key].res_ID+")'></a></td>

Code above gave me this: 上面的代码给了我这个:

<a onclick="DeleteRes(slotsPtc,327)"></a>

I'm missing single quotes around first parameter slotsPtc if anyone can help how I can put single quotes around that parameter please let me know. 如果有人可以帮助我如何在该参数周围加上单引号,我会在第一个参数slotsPtc周围缺少单引号,请告诉我。 Thanks. 谢谢。

You can insert quotes in a string by escaping them with a \\ character. 您可以通过用\\字符转义引号来在字符串中插入引号。 I recommend using escaped double quotes for your onclick attribute value and then you can use single quotes without escaping: 我建议对onclick属性值使用转义的双引号,然后可以使用单引号而不进行转义:

tbl +="<td><a onClick=\"DeleteRes('slotsPtc',"+jsData[key].res_ID+")\"></a></td>

You can use &apos; 您可以使用&apos; (or &#39; ) as a single quote in HTML: (或&#39; )作为HTML中的单引号:

tbl += "<td><a onClick='DeleteRes(&apos;slotsPtc&apos;,"+jsData[key].res_ID+")'></a></td>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM