jQuery - 鼠标悬停/悬停等不适用于新创建的元素

艾迪安鲁

我用 js 创建新元素,如下所示:

$("#vorschau_tr").append('<td><img src="bla.jpg" class="flipped js_is_a_broken_time_wasting_piece_of_garbage"></td>');

并尝试只是提醒这样的事情:

$(".js_is_a_broken_time_wasting_piece_of_garbage").on({
    mouseover: function () {
        alert('enter');
    },
    mouseleave:function () {
        alert('leave');
    }
});

我知道它被多次询问,但每次它都说解决方案是使用on我所做的但它不起作用那么如何做到这一点?

$(".js_is_a_broken_time_wasting_piece_of_garbage").live( click, function(){
    alert('js_inventor_is_a_pos');
});

也不起作用

马蒙

你已经错过了结束"img src="bla.jpg"这就是选择器没有按预期工作的原因:

$("#vorschau_tr").append('<td><img src="bla.jpg" class="flipped js_is_a_broken_time_wasting_piece_of_garbage"></td>');

$(".js_is_a_broken_time_wasting_piece_of_garbage").on({
    mouseover: function () {
        alert('enter');
    },
    mouseleave:function () {
        alert('leave');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr id="vorschau_tr"></tr>
</table>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章