【发布时间】:2012-08-21 10:07:11
【问题描述】:
我有一个通过 jQuery 将 SVG 动态添加到页面的页面:
grid.append($('<object>')
.load(function () {
// do stuff
alert('loaded')
})
.attr({
id: 'tile',
type: 'image/svg+xml',
width: Math.round(tile_w),
height: Math.round(tile_h),
data: 'map/base.svg'
})
);
我需要访问 SVG 文档(将变量“推送”到 svg 脚本上下文中),但必须为此加载 SVG。我的问题是我没有让加载事件工作。不显示任何警报。
怎么办?
编辑: 似乎 jQuery 只是阻止将“加载”事件绑定到非图像或文档元素,所以我只使用“官方”addEventListener() 函数(不是由愚蠢的IE支持,但这对我来说没问题):
grid.append($('<embed>')
.attr({
id: 'tile' + i,
type: 'image/svg+xml',
width: Math.round(tile_w),
height: Math.round(tile_h),
src: 'map/base.svg'
})
.css({
position: 'absolute',
left: Math.round(p.x),
top: Math.round(p.y)
}).each(function (i){
this.addEventListener ('load', function (e) {
alert('loaded')
})
})
);
【问题讨论】:
标签: jquery onload-event object-tag