简体   繁体   English

如何在JavaScript代码中的字符串中显示单引号?

[英]How to show single quotes in string in javascript code?

I am generation dynamic content using javascript and inside that content a part will be like 我正在使用javascript生成动态内容,在该内容中,一部分就像

onfocus="if(this.value=='Enter Email Address'){this.value='';}" onblur="if(this.value=='')    {this.value='Enter Email Address';}" value="Enter Email Address"

I want above code but after generation of the content but all the single quotes are not showing properly. 我想要上面的代码,但是在生成内容之后,但所有单引号都无法正确显示。 Please help me out. 请帮帮我。 Thanks 谢谢

If you're working in HTML 5 with input element, i would suggest you to look at the placeholder attribute, as this is exactly what you are needing for. 如果您正在使用具有input元素的HTML 5,我建议您查看placeholder属性,因为这正是您所需要的。 No javascript needed. 不需要javascript。

 <input type="text" placeholder="Enter Email Address" />

使用转义符\\

'\\'Enter Email\\''

try this 尝试这个

'\'Text\''

or 要么

"'Text'"

尝试这样的事情,从双引号中删除反斜杠, FIDDLE

<input type="text" onfocus="if(this.value=='Enter Email Address'){this.value='';}" onblur="if(this.value=='')    {this.value='Enter Email Address';}" value="Enter Email Address">

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

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