Javascript onclick 函数调用问题:不会传递字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14695149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript onclick function call issue: won't pass a string
提问by user1270657
So the basic rundown is that I'm trying to create a rudimentary means of flagging inappropriate content on our web mapping application. Within a function that dynamically creates content for the sidebar of the webmap when the user clicks on a point I have this piece of code that should generate an image of a flag.
所以基本的总结是我正在尝试创建一种在我们的网络地图应用程序上标记不适当内容的基本方法。在用户单击某个点时为 web 地图的侧边栏动态创建内容的函数中,我有这段代码应该生成一个标志的图像。
When the user clicks the flag, I want to run the function flagContent which should pass a url string into the function. From within this function I would then be able to write it to a database later on (though I haven't made it this far yet).
当用户单击标志时,我想运行函数 flagContent,它应该将一个 url 字符串传递给函数。在这个函数中,我可以稍后将它写入数据库(尽管我还没有做到这一点)。
Here are some code snippets I have been working with.:
以下是我一直在使用的一些代码片段。:
1.This is where the flag image is generated
1.这里是生成flag图片的地方
content += "<p class='info'><img id='flag' onclick='flagContent(" + attachmentInfo.url + ")
'src='assets/flag.png' style='height:15px'>Flag as inappropriate...</p>";
This is the connected function
function flagContent(imageUrl){ console.log(imageUrl)}
这是连接的功能
函数 flagContent(imageUrl){ console.log(imageUrl)}
So basically the url is a string and I'd like to be able to manipulate it within the flagContent function. Unfortunately I can't get it to work. When I pass a numerical parameter such as attachmentInfo.objectIDI do not run into the same problem.
所以基本上 url 是一个字符串,我希望能够在 flagContent 函数中操作它。不幸的是,我无法让它工作。当我传递一个数字参数(例如attachInfo.objectID)时,我不会遇到同样的问题。
For what it's worth I also get this error:
对于它的价值,我也收到了这个错误:
Uncaught SyntaxError: Unexpected token :
未捕获的语法错误:意外标记:
Any help would be greatly appreciated. Let me know if there is additional information that could help to solve this. Thanks!
任何帮助将不胜感激。如果有其他信息可以帮助解决此问题,请告诉我。谢谢!
回答by CBarr
I'm assuming that attachmentInfo.url
would return a URL, which should be a string and it just needs to be surrounded by quotes. Since you've already used both types of quotes, you will have to escape some quotes.
我假设这attachmentInfo.url
会返回一个 URL,它应该是一个字符串,它只需要用引号括起来。由于您已经使用了这两种类型的引号,因此您必须对一些引号进行转义。
content += "<p class='info'>";
content += "<img id='flag' onclick=\"flagContent('" + attachmentInfo.url + "')\" src='file.png'/>";
content += "Flag as inappropriate...";
content += "</p>";
Doing this makes the final out put look like this:
这样做会使最终的输出看起来像这样:
<p class='info'>
<img id="flag" onclick="flagContent('http://example.com')" src='file.png'/>
Flag as inappropriate...
</p>
The problem you had was that the URL was not surrounded by quotes, and it saw flagContent(http://example.com)
and didn't know what to do with those bare words not in a string.
你遇到的问题是 URL 没有被引号包围,它看到flagContent(http://example.com)
但不知道如何处理那些不在字符串中的裸词。