在javascript中使用单引号传递变量 [英] Passing variable with single quote in javascript
本文介绍了在javascript中使用单引号传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在链接上有一个onClick调用:
I have an onClick call on a link:
<a onClick="fomateName('Andrew Dsouza')"> //this is working good
问题是fomateName里面的变量会包含单引号和我的fomateName需要一个变量,如
The problem is the variable inside fomateName would contain single quotes and my fomateName Takes a variable like
var a ='Andrew D'souza'。需要格式化单引号Ex的变量;
var a='Andrew D'souza'. Need to format a variable present with single quote Ex;
<a onClick="fomateName('a')"> which turns to
<a onClick="fomateName('Andrew D'souza')"> //this is not working ,because present of single quote
任何想法如何使用适当的引号传递文本在一个javascript。
Any Idea how to pass text with proper quotes in a javascript.
单引号而不是实际名称
推荐答案
尝试:
<a onClick="fomateName('Andrew D\'souza')"> <!-- this will work -->
\
使用反斜杠来逃避'
假设你有这样的函数=>
Lets say if you have function like this =>
function fomateName(txt){
alert(txt);
}
并从锚点调用它=>
<a onClick="fomateName('Andrew D\'souza')"> <!-- this will alert "Andrew D'souza" -->
这篇关于在javascript中使用单引号传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文