简体   繁体   English

防止Javascript转义特殊字符

[英]Prevent Javascript from escaping special character

This may sound like a silly query.这听起来像是一个愚蠢的查询。 That said, I have looked all over for an answer and still cannot figure this out.也就是说,我已经四处寻找答案,但仍然无法弄清楚。

I am trying to build a dynamic link using JS, but JS keeps escaping the "/"我正在尝试使用 JS 构建动态链接,但 JS 一直在逃避“/”

Here is my code:这是我的代码:

var permalink = 'http://yvannasr.com/events';
var slug = 'run-1';
var name = 'run 1';

var link = "<a href=" + "'"+ permalink + "/" + slug +"/" +"'"+">

Here is the output I am getting for the link variable:这是我为链接变量获得的输出:

<a href=" http:="" yvannasr.com="" events="" run-1="" '="">

This is what i would like the output to be:这就是我想要的输出:

<a href="http://yvannasr.com/events/run-1/">

Could anyone let me know what i am doing wrong?谁能让我知道我做错了什么?

Many thanks, Yvan非常感谢,伊万

You are absolutely correct in your syntax, but with a small change of missing a closing " (doublequotes) in setting the "link" variable. Possibly when you render the above "link" variable as HTML you should get a similar structure as you have mentioned in your output because of a missing closing tag.您的语法绝对正确,但是在设置“链接”变量时遗漏了一个关闭的“(双引号)”(双引号)。可能当您将上述“链接”变量呈现为 HTML 时,您应该获得与您类似的结构由于缺少结束标记,在您的输出中提到。

You should be good with the following line你应该对以下行很好

var link = "<a href=" + "'"+ permalink + "/" + slug +"/" +"'"+">"

Edit: First off, your assignment of a string to variable link has bad syntax.编辑:首先,您将字符串分配给变量link具有错误的语法。 You cannot add a " character at the end the way you are. If you want to add that character, you should use '\\"' , because the backslash escapes the double-quote, allowing it to be used as a normal character.您不能按照您的方式在末尾添加"字符。如果要添加该字符,则应使用'\\"' ,因为反斜杠会转义双引号,从而可以将其用作普通字符。

I don't see how your code can possibly make the result you posted.我不知道您的代码如何可能使您发布的结果。 Especially with the equals signs all over the place;尤其是到处都是等号; it makes no sense to me.对我来说完全是无稽之谈。

When I run this code:当我运行此代码时:

var x = "http://www.google.com"; x;

The result is:结果是:

"http://www.google.com"

The forward slash can be used in strings without any escaping or workaround.正斜杠可以在字符串中使用而无需任何转义或解决方法。

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

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