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')
})
})
);