未终止字符串的 JavaScript 错误

发布于 2024-09-03 04:17:15 字数 901 浏览 4 评论 0原文

我想传递一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谈场末日恋爱 2024-09-10 04:17:15

好的,我有解决方案:

<element onMouseover="showhint('<p>Hemmed Finish: Every side/edge (1/2" to 2") of the banner is folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.</p><p>Stitched Finish: Every side/edge (1" to 2") of the banner is folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner</p>', this, event, '250px')" />

您必须使用 " 以 XML 样式转义引号。

请参阅此处的工作示例。

Ok, I have the solution:

<element onMouseover="showhint('<p>Hemmed Finish: Every side/edge (1/2" to 2") of the banner is folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.</p><p>Stitched Finish: Every side/edge (1" to 2") of the banner is folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner</p>', this, event, '250px')" />

You have to escape the quotes XML-style, using ".

See a working example here.

南七夏 2024-09-10 04:17:15

我在这里看到两个潜在的问题。

  1. JavaScript 和许多其他编程语言一样,使用 " 字符来标记字符串的开头和结尾。你的提示框文本中包含一个 ",所以当 JavaScript 遇到它时,它会认为你的字符串已经结束。要解决此问题,您应该将字符串中的每个“引用为\”。这告诉 JavaScript 您不打算结束字符串,而是计划在字符串中放置一个 "。

  2. 同样,JavaScript 不允许您直接在字符串内放置“段落分隔符”。相反,每次您希望在字符串中包含回车符时,都必须键入 \n。

所以,这里是你应该如何定义你的提示框字符串:

hintbox = "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.\n\nStitched 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.";

这通常被称为“转义字符串”,快速的谷歌应该会给你更多关于该主题的信息。

快乐编程!

I see two potential problems here.

  1. 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.

  2. 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:

hintbox = "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.\n\nStitched 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 is typically called "escaping strings", and a quick Google should give you much more information on the toppic.

Happy programming!

客…行舟 2024-09-10 04:17:15

检查这一点:

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')"

您不需要转义:括号如 (

<script type="text/javascript">
<!--//
function alert2()
{
alert ("Don't forget the curly "+
"brackets - \nThey are essential!");
}
//-->
</script>

Check 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')"

you do not need to escape : brackets like (

<script type="text/javascript">
<!--//
function alert2()
{
alert ("Don't forget the curly "+
"brackets - \nThey are essential!");
}
//-->
</script>
夜吻♂芭芘 2024-09-10 04:17:15

虽然这个例子对我有用,但它非常丑陋。避免内联事件处理程序以及随之而来的疯狂转义问题。将数据放在其他地方,例如适当的 JS 变量或属性值中。对于工具提示的情况,title 属性是最好的:

<span title="Hemmed Finish: Every side/edge (½″ 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.">
    finish options...
</span>

虽然它本身可以用作工具提示,但如果您愿意,您可以使用渐进增强功能将本机 HTML 工具提示替换为脚本化工具提示。随之而来的更灵活的样式:

var spans= document.getElementsByTagName('span');
for (var i= spans.length; i-->0;)
    if (spans[i].title)
        replaceTitle(spans[i]);

function replaceTitle(element) {
    var title= element.title;
    element.title= '';
    element.onmouseover= function(event) {
        showhint(title, this, event||window.event, '250px');
    };
    element.onmouseout= function(event) {
        hidehint(...);
    };
}

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:

<span title="Hemmed Finish: Every side/edge (½″ 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.">
    finish options...
</span>

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:

var spans= document.getElementsByTagName('span');
for (var i= spans.length; i-->0;)
    if (spans[i].title)
        replaceTitle(spans[i]);

function replaceTitle(element) {
    var title= element.title;
    element.title= '';
    element.onmouseover= function(event) {
        showhint(title, this, event||window.event, '250px');
    };
    element.onmouseout= function(event) {
        hidehint(...);
    };
}
心如荒岛 2024-09-10 04:17:15

您必须转义“字符。因此,不要使用“\”(反斜杠)。您可以使用换行符来创建段落\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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文