Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于jquery切换父元素的增加_Jquery_This_Parent - Fatal编程技术网

关于jquery切换父元素的增加

关于jquery切换父元素的增加,jquery,this,parent,Jquery,This,Parent,只有第一个元素可用,添加元素不可用 这是因为您正在动态生成元素。您应该委派活动: $(".user_img").click(function() { if ($(".user-insert-image").css("display") == 'none') { $(".user-upload-image").hide(); $(".user-insert-image").show(); } else { $(".user-insert-image").hide();

只有第一个元素可用,添加元素不可用


这是因为您正在动态生成元素。您应该委派活动:

$(".user_img").click(function() {
if ($(".user-insert-image").css("display") == 'none') {
    $(".user-upload-image").hide();
    $(".user-insert-image").show();
} else {
    $(".user-insert-image").hide();
    $(".user-upload-image").show();
} });
您还需要使用
这个
关键字,您当前的逻辑隐藏/显示所有元素

$('.main').on('click', '.user_img', function() {

谢谢,老兄,但是第一个有一些错误,你能修复吗?
$('.main').on('click', ".user_img", function() {
    var $this = $(this).parent();
    if ($this.hasClass('user-insert-image')) {
        $this.hide().prev().show()
    } else {
        $this.hide().next().show()
    }
});