1
jQueryを使用して<svg>
を作成し、<div>
に追加しています。jQueryを使用して新しく作成したSVG要素にイベントをバインドする方法は?
append()
の後にeach()
を使用して<svg>
にアクセスできますが、ハンドラは機能しません。あらかじめ作成しておけばそれがあります。
$(document).ready(function() {
$('#testbtm').click(function() {
var svgElement = $('<svg class="hexagon" class="ui-widget-content">\
<text fill="black" x=75 y=75 style="text-anchor: middle">1</text>\
<path d="M38 0 L113 0 L150 65 L113 130 L38 130 L0 65 Z"/fill="none" stroke="blue"></svg>');
svgElement.children('text').text(1);
svgElement.attr("class", "hexagon ui-widget-content");
$("#display").append(svgElement);
}); //end click
$('#testbtm2').click(function() {
$('.hexagon').each(function() {
$(this).children('text').text('hi');
});
}); // end click
$('.hexagon').click(function() {
$(this).children('path').css('fill', 'blue');
}); //end click
}); // end ready
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="display">
<svg class="hexagon" class="ui-widget-content">
<text fill="black" x=75 y=75 style="text-anchor: middle">1</text>
<path d="M38 0 L113 0 L150 65 L113 130 L38 130 L0 65 Z"/fill="none" stroke="blue">
</svg>
</div>
<button id="testbtm">test</button>
<button id="testbtm2">test2</button>
あなたがまだ作成されていない何かにあなたのクリックハンドラをバインドする.on http://api.jquery.com/on/を使用しなければならないので、私はそのかなり確実だろう。 – dmoo
この回答をお試しください>> http://stackoverflow.com/questions/28052506/function-doesnt-work-after-appending-new-element –