3 jquery
是否有可能在jQuery中添加元素的事件?例如,如果我想在单击时向元素添加.fadeIn(),但也保留可能已存在的任何其他功能(假设我不知道当前是否有任何触发点击).
jQuery不会覆盖事件,它会添加到它们中.所以,如果你这样做:
$(document).ready(function() {
$("#test").click(function () { $(this).append("test<br />"); });
$("#test").click(function () { $(this).append("test 2<br />"); });
});
Run Code Online (Sandbox Code Playgroud)
div看起来像:
<div id="test">click me <br /></div>
Run Code Online (Sandbox Code Playgroud)
当你点击div时,它会同时附加"test"和"test 2".