我在 JavaScript 中是否错误地转义了字符串?

发布于 2024-12-27 16:19:34 字数 287 浏览 1 评论 0原文

我试图在 javascript 中创建一个可点击的链接

var content = '<a href=\"#\" onclick=\"displayContent(\"TEST\")\">Whatever</a>';

$("#placeHolder").html(content);

,但我不断收到错误

未捕获的语法错误:意外的标记}

这不是转义双引号并创建链接的正确方法吗?

I am trying to create a clickable link in javascript

var content = '<a href=\"#\" onclick=\"displayContent(\"TEST\")\">Whatever</a>';

$("#placeHolder").html(content);

but I keep getting an error

Uncaught SyntaxError: Unexpected token }

Is that not the correct way to escape double quotes and create a link?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

冷了相思 2025-01-03 16:19:34

您只需要转义单引号

var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>'

正如 bozdoz 所说:

对单引号内的单引号进行转义;您转义双引号内的双引号


但为什么不这样做

var content = $("<a />").attr("href", "#").text("Whatever").click(function(){
     displayContent('TEST')
});

Or 正如内森所说:

var content = $('<a href="#">Whatever</a>').click(
     function() { displayContent('TEST') 
});

You only need to escape the single quotes

var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>'

As bozdoz says:

You escape single quotes that are within single quotes; you escape double quotes that are within double quotes


But why not do

var content = $("<a />").attr("href", "#").text("Whatever").click(function(){
     displayContent('TEST')
});

Or as Nathan says:

var content = $('<a href="#">Whatever</a>').click(
     function() { displayContent('TEST') 
});
迷爱 2025-01-03 16:19:34

您可以通过创建如下元素来避免这种混乱:

var content = $( "<a>", {
    href: "#",
    text: "whatever",
    click: $.proxy( displayContent, 0, "TEST" )
});

You can avoid that mess by creating elements like this:

var content = $( "<a>", {
    href: "#",
    text: "whatever",
    click: $.proxy( displayContent, 0, "TEST" )
});
一人独醉 2025-01-03 16:19:34

仅当引号与开盘价和收盘价所使用的类型相同时,您才需要转义引号。在您的示例中,您不必要地转义双引号,因为您的字符串用单引号引起来。话虽这么说,由于 onclick 语句中的双引号,解析器在调用 displayContent() 时会出现问题。

试试这个:

var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>'; 

You only need to escape quotes when they are the same type as used by your opening and closing quotes. In your example, you are unnecessarily escaping double quotes because your string is wrapped in single quotes. That being said, because of the double quotes in onclick statement the parser will have issues with the the call to displayContent().

Try this, instead:

var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>'; 
原来分手还会想你 2025-01-03 16:19:34

你根本不需要逃避他们!

var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>'; 

$("#placeHolder").html(content); 

就像这样,因为通常,当嵌入 ' 时,您不需要转义 ";

但您确实需要转义 ' 字符。

You don't need to escape them at all!

var content = '<a href="#" onclick="displayContent(\'TEST\')">Whatever</a>'; 

$("#placeHolder").html(content); 

Just have it like that, as you don't need to escape " usually, when in embeded in a ';

But you do need to escape ' characters when it is a parameter in a function.

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