Javascript代码问题"和& # 39;

Javascript code problems with " and '

本文关键字:quot 代码 问题 Javascript      更新时间:2023-09-26

通常我们可以使用var a = "<a href='index.php'>index</a>";,但我需要在同一行代码中有超过2个',如下所示:

var status = "<a href='javascript:window.open('atendimento.php','hhchat','top=70,left=70,width=480,height=450,status=no,location=no,statusbar=no');void(0);'><img src='img/atendimento.png' width='160' border='0' /></a>";

所以问题是它在这里开始和结束行代码:'javascript:window.open('

我需要它一直持续到行尾。怎样才能做到呢?

使用"并转义:

var status = "<a href='javascript:window.open('"atendimento.php'",'"hhchat'", ...

虽然这是一个完美的例子,为什么你应该使用不显眼的JavaScript绑定事件- http://en.wikipedia.org/wiki/Unobtrusive_JavaScript,你不会有这种问题。

你需要转义你的字符:

var status = "<a href='"javascript:window.open('atendimento.php','hhchat','top= ...

更多信息在W3Schools:http://www.w3schools.com/js/js_obj_string.asp

使用反斜杠转义字符串中的引号:

"<a href='javascript:window.open('"atendimento.php'" ....

或者更好,考虑最佳实践,即避免将JS代码内联到HTML中。

相反,你应该考虑在单独的JS代码中为你的元素定义一个click事件。这将消除嵌套引号的整个问题。

在href=

后面使用如下转义字符
var status = "<a href='"javascript:window.open('atendimento.php','hhchat','top=70,left=70,width=480,height=450,status=no,location=no,statusbar=no');void(0);'"><img  src='img/atendimento.png' width='160' border='0' /></a>"; 

交换" and "然后转义'"

var status = "<a href='javascript:window.open('"atendimento.php'"...