Jquery悬停函数不会在动态添加的元素上运行

use*_*007 1 javascript jquery

这成功,

$('[rel=notif]').on({
    mouseenter: function () {
$(this).find('.solnotifkara').css({"opacity":"0.46"});
  },
    mouseleave: function () {
$(this).find('.solnotifkara').css({"opacity":"0.36"});
    }
});
Run Code Online (Sandbox Code Playgroud)

但是,如果它动态添加,则无法正常工作.我有一个点击功能,适用于动态添加的元素,

$(document.body).on('click', '[rel="like"]' ,function(){

alert("works");

});
Run Code Online (Sandbox Code Playgroud)

如何使悬停功能适用于动态添加的元素?

Rok*_*jan 6

$(document).on({
  mouseenter: function () {
    $(this).find('.solnotifkara').css({opacity:0.46});
  },
  mouseleave: function () {
    $(this).find('.solnotifkara').css({opacity:0.36});
  }
}, "[rel=notif]");  // <<<<< delegate here your dynamic element
Run Code Online (Sandbox Code Playgroud)

$(document).on({
  mouseenter: function () {
    $(this).find('.solnotifkara').css({opacity:0.2});
  },
  mouseleave: function () {
    $(this).find('.solnotifkara').css({opacity:1});
  }
}, "[rel=notif]");  // <<<<< delegate here your dynamic element


// for test only:
$("button").on("click", function(){
  $("body").append('<p><a rel="notif">This is a <span class="solnotifkara">note</span>.</a></p>');
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button>GENERATE</button>
Run Code Online (Sandbox Code Playgroud)