如何正确逃逸此功能? [英] How to do a Proper Escape of this Function?
问题描述
我有一个从jQuery生成的DIV
标记,如下所示:
I have a DIV
tag generated from jQuery that goes like this:
$('#results')
.append('<DIV id='
+ A.pid
+ ' onmouseover=function(){google.maps.event.trigger(marker, 'mouseover');};><H3>'
+ A.name
+ '</H3></DIV>');
Firebug给我一个错误列表后缺少参数",因为它无法立即识别'mouseover'
之后的)
.
Firebug is giving me an error "missing ) argument after list" as it does not recognise the )
immediately after 'mouseover'
.
将DIV
悬停在鼠标上时,执行\'mouseover\'
会产生语法错误.语法错误显示为: function(){google.maps.event.trigger(marker
并查看生成的DIV显示:
Doing a \'mouseover\'
produces a syntax error upon mouseover of the DIV
. The syntax error reads: function(){google.maps.event.trigger(marker,
and a look at the generated DIV shows:
<div id="1" 'mouseover');};="" onmouseover="function(){google.maps.event.trigger(marker,">
执行"mouseover"
会生成空白文档.
如何对此功能进行适当的转义?
How do I do a proper escape for this function?
更新:
这应该有效:
$('#results')
.append('<DIV id='
+ A.pid
+ ' onmouseover=\"function(){google.maps.event.trigger(marker, \'mouseover\');};\"><H3>'
+ A.name
+ '</H3></DIV>');
我需要在函数中使用转义的双引号,并在参数中使用转义的单引号.
I need to put escaped double quotes for the function and escaped single quotes for the argument.
推荐答案
您需要在HTML属性的值周围放置".否则浏览器会认为该属性在下一个空格处结束,并且恰好在
You need to put " around the values of your html attributes. Otherwise the browser thinks the attribute ends at the next whitespace and that is exactly right after the (marker,
part.
$('#results')
.append('<DIV id="'
+ A.pid + '"'
+ ' onmouseover="function(){google.maps.event.trigger(marker, \'mouseover\');};"><H3>'
+ A.name
+ '</H3></DIV>');
这篇关于如何正确逃逸此功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!