简体   繁体   English

如何将锚标记格式化为字符串?

[英]How to format an anchor tag as a string?

this line: 这行:

return '<a href="javascript:SomeFunction('+ var1 + ',' + var2 + ')"; > Stars </a>';

renders this: 呈现此:

<a href="javascript:SomeFunction(Pure Magic,4)" ;=""> Stars </a>

which looks like a proper anchor tag but gives this error on clicking it: 看起来像是正确的锚标记,但是单击它会出现此错误:

Uncaught SyntaxError: missing ) after argument list 参数列表后未捕获到的SyntaxError:缺少)

I am sure this error is misleading as the bracket in question is right there. 我肯定这个错误会引起误解,因为有关的括号就在那里。

What am I missing? 我想念什么?

Thanks in advance. 提前致谢。

As var1 contains a string value, you need quotes around it: 由于var1包含一个字符串值,因此需要在其周围var1引号:

return '<a href="javascript:SomeFunction(\'' + var1 + '\',' + var2 + ');"> Stars </a>';

If the string can contains characters that needs to be escaped to be in a string literal, or needs to be URI encoded, you need some more code: 如果字符串可以包含需要转义为字符串文字或需要URI编码的字符,则需要更多代码:

return '<a href="javascript:' + encodeURIComponent('SomeFunction(\'' + var1.replace("\\", "\\\\").replace("'", "\\'") + '\',' + var2 + ');') + '"> Stars </a>';

Generating code like this is complicated, and it's easy to get it wrong. 生成这样的代码很复杂,而且很容易出错。 If possible you should generate elements instead, so that you can set the properties directly instead of creating code for it. 如果可能,应改为生成元素,以便可以直接设置属性,而不必为其创建代码。 Example using jQuery: 使用jQuery的示例:

return $('<a>', { href: '#', text: ' Stars ' }).click(function(e){
  e.preventDefault();
  SomeFunction(var1, var2);
});

您在双引号结尾有误

return '<a href="javascript:SomeFunction('+ var1 + ',' + var2 + ');" > Stars </a>';

You are not wrapping your first var (which seems to be a string) within quotes. 您没有将第一个变量(似乎是一个字符串)用引号引起来。

return '<a href="javascript:SomeFunction(\''+ var1 + '\', ' + var2 + ');" > Stars </a>';

NOTE : As pointed out in the comments by Paul S. the semicolon should be inside the double quotes as it is part of the attribute value. 注意 :如Paul S.注释所指出,分号应位于双引号内,因为它是属性值的一部分。

As you just want to add an <a> with a href , let the browser do the escaping and encoding work for you by using a method like String.prototype.link 正如您只想添加带有href<a> ,让浏览器使用String.prototype.link类的方法为您完成转义和编码工作。

' Stars '.link('javascript:SomeFunction(' + JSON.stringify(var1) + ', ' + JSON.stringify(var2) + ')');
// <a href="javascript:SomeFunction(&quot;Pure Magic&quot;, 4)"> Stars </a>

If you're running into issues like this a lot, consider whether it would be easier to switch to working with DOM methods , ie 如果您经常遇到此类问题,请考虑是否更容易切换到使用DOM方法 ,即

var a = document.createElement('a');
a.addEventListener('click', SomeFunction.bind(a, var1, var2));

This saves you from having to write entity encodings etc to protect yourself at each level the code will be interpreted ( JavaScript , HTML , JavaScript ) and means you have more direct access over the DOM tree than by writing HTML 这使您不必编写实体编码等来保护自己在代码将被解释的每个级别( JavaScriptHTMLJavaScript ),并且意味着与编写HTML相比,您对DOM树的访问更加直接

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

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