将引号传递给javascript函数内html元素内的onclick事件 [英] passing quotes to an onclick event within an html element within a javascript function
问题描述
这似乎比我见过的类似帖子复杂了一步:
我在js函数中创建了以下行:
This seems to be one step more complicated than similar posts I've seen:
I created the following line within a js function:
content.push("<input type='button' value='Use This Location' onclick='alert(" + result.location.y + "," + result.location.x + ")'/>");
结果是一个带有以下内容的按钮:
onclick="alert(40.7445068,-73.9834671)"
The result is a button with the following:
onclick="alert(40.7445068,-73.9834671)"
但是我希望结果是:
onclick="alert('40.7445068,-73.9834671')"
but I want the result to be:
onclick="alert('40.7445068,-73.9834671')"
或..您知道..只是..但是要使警报显示文本,而不仅仅是第一个数字.
or..you know..just.. however it would take to make the alert show the text, not just the first number.
如何重新格式化原始块中的引号以实现这种情况?
How can I reformat the quotes within the original block to make that happen?
答案:
非常感谢大家!您的输入(无意输入)导致了可行的解决方案:
content.push('<input type="button" value="Use This Location" onclick="alert(''+ result.location.y + "," + result.location.x + '')" />');
Thanks so much folks! Your input (no put intended) led to a workable solution:
content.push('<input type="button" value="Use This Location" onclick="alert(''+ result.location.y + "," + result.location.x + '')" />');
因为它是javascript中HTML内的javascript,所以我将push方法作为单引号而不是原始的双引号传递,因此我能够使html元素双引号和onclick单引号.然后,我必须使用'
而不是"
,这样我才能在HTML中插入特殊的单引号.
Since it's javascript within HTML within javascript, I passed the push method as single quotes instead of the original double ones, then I was able to make the html element double quotes and the onclick single. Then I had to use '
instead of "
so I could get a special single quote inserted within the HTML.
再次,非常感谢.所有评论都是解决问题的关键,非常感谢!
Again, thanks very much. All the comments were key in solving / I am grateful!
PS:很抱歉要通过编辑回答:显然,由于我的声誉有限,我不得不再等7个小时才能正式回答自己的问题.
PS: Sorry for answering through an edit: Apparently I have to wait 7 more hours to officially answer my own question because of my limited reputation.
推荐答案
content.push("<input type='button' value='Use This Location' onclick='alert(\"" +
result.location.y + ',' + result.location.x + "\")'/>");
修正了一些代码.
这篇关于将引号传递给javascript函数内html元素内的onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!