我正在写一个 jquery 插件。
是否可以像下面这样编写/添加事件“点击”,因为我要添加事件的 html 正在被动态注入
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://codeorigin.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
/* To be moved to own custom js plugin file ------------------------------------------- */
(function ($) {
$(document).ready(function () {
//add events
$('body').on('click', '.injectedSpan', function () {
//alert($(this).html());
$(this).remove();
});
//custom plugin
$.fn.addMySpan = function () {
return this.each(function () {
$(this).append('<span style="cursor:pointer" class="injectedSpan"> added</span>');
});
};
});
}(jQuery));
/* -----------------------------------------------------------------------------------------------------*/
$(document).ready(function () {
$('#One,#Two,#Three').addMySpan();
});
</script>
</head>
<body>
<form id="form1" >
<div>
<div id="One">test1</div>
<br />
<br />
<br />
<div id="Two">test2</div>
<br />
<br />
<br />
<div id="Three">test3</div>
</div>
</form>
</body>
</html>