HTML
<div id="header">top of page</div>
<div class="message">hello, how are you</div>
<div class="message">I am fine</div>
jquery
$(document).ready(function () {
$("#header").append('<div class="message">appended DIV</div>');
$('div.message').hover(
function () {
alert('hi');
},
function () {
alert('bye');
}
);
});
HTML 内の既存の DIV がマウスをトリガーしてアラートを出すのに、追加された DIV がそうではなく、「死んでいる」のはなぜですか。追加された DIV は、既存の DIV のようにどのように反応しますか?
編集:要約すると、これが解決策でした:
$('<span />').addClass('message').text('(mouse 1)').appendTo('#header');
$('<span />').addClass('message').text('(mouse 2)').appendTo('#header');
$('.message').hover(
function () {
$('#header').prepend('in');
},
function () {
$('#header').prepend('out');
}
);
SPAN がホバー機能の下に配置されている場合、機能しないことに注意してください。