jquery append元素的点击事件无效
代码:
$(".myDiv").click(function(){
$(".myDiv").append($("<p class='hello'>hello!</p>"));
})
$(".hello").click(function(e){
e.stopPropagation();
alert("hello")
})
点击p元素时并不会弹出hello!
$(".myDiv").click(function(){
$(".myDiv").append($("<p class='hello'>hello!</p>"));
})
$(".myDiv").on("click",".hello",function(e){
e.stopPropagation();
alert("hello")
})
现在可以啦!
注意:click是点击事件,但是在页面加载完之后,jquery事件新添加的元素,通过click的话是无法获取元素的,这个时候要用on去获取元素事件,简单的说页面加载完成时候页面显示的元素可以用on,也可以用click,但是页面加载完成之后后期再追加的元素只能用on。(在jQuery1.7版本中 bind()、live() 和 delegate()被on代替)
jQuery append($(this))原来的元素会被删除
直接使用 append ((this))这样的方法,jquery会吧(this)) 这样的方法,jquery会吧(this))这样的方法,jquery会吧(this)元素直接从原来的地方删除,再添加到KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲target")元素中。 如果…(this)原来的位置需要使用append($(this).clone())方法.
jquery 获取所有子孙元素及所有父元素
$(selector).find(selector)
$(selector).parents()
jquery中的next()and prev()
在写轮播图时发现jQuery的prev()可以获取下一个兄弟元素,于是就想:
$(".btn_prev").click(function(){
clearInterval(interval);
$prev=$number.filter(".now").prev();
index=$prev.index();
changeImg();
});
当我用点击上一张图片按钮是,是一直循环的,但是next()就不是这样了,当当前元素时最后一个是不会返回到第一个元素,于是:
$(".btn_next").click(function(){
clearInterval(interval);
$next=$number.filter(".now").next();
if(index>3){
index=0;
}else{
index=$next.index();
}
changeImg();
});
建议还是用原生js写吧。