未终止字符串的 JavaScript 错误
我想传递一个 javascript functiona 的参数,它是一个字符串。这个javascript函数是鼠标悬停时的提示框..
我使用的字符串是这样的:
包边饰面:每侧/边缘 (1/2" 至 2") 的横幅被折叠并 粘合(特殊乙烯基溶液)或加热 按下。这是最常见且 最佳完成选项。
缝合表面:每侧/边缘 (1" 到 2") 的横幅折叠在 背面并用白色缝合/缝制 线。这不是一个常见的选项,因为 横幅上可以看到线程。
现在,在鼠标悬停的提示框中,必须显示上面给定的文本,因为它与段落分隔符一起显示在上面。但是当我将上面的内容作为该函数中的参数传递并附加一些反斜杠来识别一些标点符号时,iots 直到给出我的 javascript 错误是未终止的字符串...
我正在这样做:
onMouseover="showhint('Hemmed Finish\: Every side/edge \(1/2\'\' to 2\'\'\) of the banner are folded and glued \(special vinyl solution\) or heat pressed. This is the most common and best finish option.Stitched Finish\: Every side/edge \(1\'\' to 2\'\'\) of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner', this, event, '250px')"
请你帮我纠正这个问题...
I want pass a parameter of javascript functiona which is a string. This javascript function is a hintbox on mousehover..
and the string i am using is like this:
Hemmed Finish: Every side/edge (1/2"
to 2") of the banner are folded and
glued (special vinyl solution) or heat
pressed. This is the most common and
best finish option.Stitched Finish: Every side/edge (1"
to 2") of the banner are folded in the
back and stitched/sewed with white
thread. This is not a common option as
thread can be seen on the banner.
Now in the hintbox on mousehover the above given text has to be display as it is displayed above along with the paragraph break.. But when i pass the above as parameter in that function along with appending some backslashes to recognise some punctuation, iots till gives me javascript error of unterminated string...
I am doing this:
onMouseover="showhint('Hemmed Finish\: Every side/edge \(1/2\'\' to 2\'\'\) of the banner are folded and glued \(special vinyl solution\) or heat pressed. This is the most common and best finish option.Stitched Finish\: Every side/edge \(1\'\' to 2\'\'\) of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner', this, event, '250px')"
pls could u help me in rectifying the issue...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好的,我有解决方案:
您必须使用
"
以 XML 样式转义引号。请参阅此处的工作示例。
Ok, I have the solution:
You have to escape the quotes XML-style, using
"
.See a working example here.
我在这里看到两个潜在的问题。
JavaScript 和许多其他编程语言一样,使用 " 字符来标记字符串的开头和结尾。你的提示框文本中包含一个 ",所以当 JavaScript 遇到它时,它会认为你的字符串已经结束。要解决此问题,您应该将字符串中的每个“引用为\”。这告诉 JavaScript 您不打算结束字符串,而是计划在字符串中放置一个 "。
同样,JavaScript 不允许您直接在字符串内放置“段落分隔符”。相反,每次您希望在字符串中包含回车符时,都必须键入 \n。
所以,这里是你应该如何定义你的提示框字符串:
这通常被称为“转义字符串”,快速的谷歌应该会给你更多关于该主题的信息。
快乐编程!
I see two potential problems here.
JavaScript, like many other programming languages, uses the " character to mark the beginning and the end of a string. Your hintbox text contains a ", so when JavaScript encounters it, it thinks that your string has ended. To fix the problem, you should refer to each " in your string as \". This tells JavaScript that you don't intend to end the string, but rather you plan to put a " inside your string.
Similarly, JavaScript does not allow you to place a "paragraph break" directly inside a string. Instead, you must type \n each time you want a carriage return inside your string.
So, here is how you should define your hintbox string:
This is typically called "escaping strings", and a quick Google should give you much more information on the toppic.
Happy programming!
检查这一点:
您不需要转义:括号如 (
Check this :
you do not need to escape : brackets like (
虽然这个例子对我有用,但它非常丑陋。避免内联事件处理程序以及随之而来的疯狂转义问题。将数据放在其他地方,例如适当的 JS 变量或属性值中。对于工具提示的情况,
title
属性是最好的:虽然它本身可以用作工具提示,但如果您愿意,您可以使用渐进增强功能将本机 HTML 工具提示替换为脚本化工具提示。随之而来的更灵活的样式:
Whilst the example works for me, it's pretty ugly. Avoid inline event handlers and the crazy escaping problems that come with them. Put the data elsewhere, such as in a JS variable or an attribute value where appropriate. For the case of a tooltip, the
title
attribute is best:Whilst that works as a tooltip in itself, you can use progressive enhancement to replace that native-HTML tooltip with a scripted one if you want eg. the more flexible styling that comes with it:
您必须转义“字符。因此,不要使用“\”(反斜杠)。您可以使用换行符来创建段落\n,或者如果您的工具提示能够显示html,请使用
或
作为段落。
我建议您还查看 jquery 工具提示 工具提示。
You have to escape " characters. So instead of " use \" (backslash). You could use the new line character to create the paragraph \n or if your tooltip is capable of displaying html use
or
for the paragrahps.
I suggest you also check out jquery tooltip Tooltip.