admin管理员组文章数量:1279114
I'm going to preface this with: "I know this is bad practice and an ugly hack (and I'm sorry) but..."
I'm using jQuery TOOL's tooltip widget to display a tooltip on an html element when the user hovers over it. With this widget you add the tooltip's html to the element's title
attribute.
Inside of that html I have an element onto which I want to bind an inline onclick
event handler.
Unfortunately I've run into too many layers of quotes to pass a parameter to the function I'm trying to call.
I have something like this:
<div title="<div onclick='myFunction(_____)'>My tooltip content</div>">My element</div>
This works if I need to pass an integer to myFunction
since it doesn't need another set of quotes. Unfortunately I want to pass a string to myFunction
. How can I further escape this string parameter so that it doesn't close the onclick
or the title
string?
I'm going to preface this with: "I know this is bad practice and an ugly hack (and I'm sorry) but..."
I'm using jQuery TOOL's tooltip widget to display a tooltip on an html element when the user hovers over it. With this widget you add the tooltip's html to the element's title
attribute.
Inside of that html I have an element onto which I want to bind an inline onclick
event handler.
Unfortunately I've run into too many layers of quotes to pass a parameter to the function I'm trying to call.
I have something like this:
<div title="<div onclick='myFunction(_____)'>My tooltip content</div>">My element</div>
This works if I need to pass an integer to myFunction
since it doesn't need another set of quotes. Unfortunately I want to pass a string to myFunction
. How can I further escape this string parameter so that it doesn't close the onclick
or the title
string?
- 2 Check out a similar question here: stackoverflow./questions/2004168/javascript-escape-quotes – wilsjd Commented Apr 2, 2013 at 22:27
- Thanks @wilsjd, that indeed is a working solution. – Brad Dwyer Commented Apr 2, 2013 at 22:34
2 Answers
Reset to default 9Inside of HTML attributes, you should encode quotes as HTML entities, e.g.:
<div title="This says "Hello!"">
Hello!
</div>
I was able to find a solution to my particular problem. Not sure if this works in the general case or if jQuery TOOLs is doing something magical to unescape my string but I ended up escaping with "
and it did evaluate into valid Javascript that was executed.
Something like this:
<div title="<div onclick='myFunction("_____")'>My tooltip content</div>">My element</div>
I don't really understand how this is working to be honest. Would love if someone could clarify what part of the process is changing those "
s into actual functional quotes.
本文标签: jqueryThird level of quote escaping in HTML and JavaScriptStack Overflow
版权声明:本文标题:jquery - Third level of quote escaping in HTML and JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741283867a2370164.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论