简体   繁体   English

将click函数委托给附加的div

[英]Delegating click function to appended div

I am having trouble understanding how to attach a click function to an element inside my appended portion. 我在理解如何将单击功能附加到附加部分内的元素时遇到了麻烦。 I looked through the answers of previous question but can't figure out out to apply it to my code. 我浏览了上一个问题的答案,但无法弄清楚将其应用于我的代码。 Can anyone help? 有人可以帮忙吗? What I am trying to achieve: When the user clicks on news, a news feed toggles down. 我要实现的目标:当用户单击新闻时,新闻提要向下切换。 The sections of news seen can be clicked to remove the current toggle into a new section (concerts). 可以单击看到的新闻部分以将当前切换切换到新部分(音乐会)。

edit: The problem is that the 'news feed' is inside a container. 编辑:问题是“新闻提要”在容器内。 When the user clicks a menu tab either box-toggle 1,2,3, etc will remove the contents of the container (news feed) and replace it with the new appended box-toggle. 当用户单击菜单选项卡时,无论是按框切换的1,2,3,还是其他,都将删除容器(新闻提要)的内容,并将其替换为新的附加框切换。 Im trying to get the news1clone to act in the same manner. 我试图让news1clone采取相同的行动。 Deleting in the contents (news feed) and appending the appropriate box toggle. 删除内容(新闻源)并添加相应的框切换。

 $('#news').click(function() {
     $('.nav li').removeClass('active');    
        $(this).addClass('active');
    $('.box-toggle').remove();
    $('.box-toggleclone').remove();
    $('.box-toggle1').remove();
    $('.box-toggle2').remove();
    $('.box-toggle3').remove();
    $('#box').removeClass('box-zoom');
    $('#box').addClass('box-reg');
    $('.placer2').append('<div class = "box-toggleclone">'+
        '<div class = "newsbodyclone">'+
        '<div class = "newsheaderclone"><h1>News</h1></div>'+
        '<div class = "news1clone">'+
                    '<div class = "news1clone-photo">'+
                    '</div>'+
                    '<div class = "news1clone-info">'+
                        '<p>MAY XX, 2015</p>'+
                        '<h2>OPENING<br>'+
                        'SPRING FASHION PARTY<h2>'+
                    '</div>'+
        '</div>'+
           '<div class = "news2clone">'+
                    '<div class = "news2clone-photo">'+
                    '<img src="album-cover/tyler.jpg" alt = "photo-carosel">'+
                    '</div>'+
                    '<div class = "news2clone-info">'+
                        '<p>MAY 28, 2015</p>'+
                        '<h2>TYLER THE CREATOR<br>'+
                        'Primavera Sound Concert<h2>'+
                    '</div>'+
        '</div>'+
           '<div class = "news3clone">'+
                    '<div class = "news3clone-photo">'+
                    '<img src="album-cover/jcole.jpg" alt = "photo-carosel">'+
                    '</div>'+
                    '<div class = "news3clone-info">'+
                        '<p>J. Cole, Latest album</p>'+
                        '<h2>2014 Forest Hills Drive<br>'+
                        '(2014)<h2>'+
                    '</div>'+
            '</div>'+
        '</div>'+
        '</div>');
$('.placer2').delegate('click','.news2clone',(function() {
         $('.box-toggle').remove();
        $('.box-toggleclone').remove();
        $('.box-toggle1').remove();
        $('.box-toggle2').remove();
        $('.box-toggle3').remove();
        $('#box').removeClass('box-zoom');
        $('#box').addClass('box-reg');
        $('.placer2').append('<div class = "box-toggle2">'+
            '<div class = "concert-body">'+
            '<div class = "concert1">'+
                        '<div class = "concert1-photo">'+
                        '<img src="album-cover/dmx.jpg" alt = "photo-carosel">'+
                        '</div>'+
                        '<div class = "concert1-info">'+
                            '<p>Sunday, May 24 2015</p>'+
                            '<h2>DMX CONCERT<br>'+
                            '<h2>'+
                        '</div>'+
                '</div>'+
                  '<div class = "concert2">'+
                        '<div class = "concert2-photo">'+
                        '<img src="album-cover/tyler.jpg" alt = "photo-carosel">'+
                        '</div>'+
                        '<div class = "concert2-info">'+
                            '<p>Thursday, May 28 2015</p>'+
                            '<h2>TYLER THE CREATOR<br>'+
                            'Primavera Sound<h2>'+
                        '</div>'+
                '</div>'+
            '</div>'+
            '</div>');
          })
        );
          });

I would recommend using jquery's 'on' method instead of delegate. 我建议使用jquery的“ on”方法代替委托。 The trick here is setting the correct context. 这里的窍门是设置正确的上下文。 The first selector needs to be an element that is there on page load and not ajaxed in or injected later. 第一个选择器必须是页面加载时存在的元素,并且以后不被取消或注入。 Try adding the 'document' as the context and it should work. 尝试添加“文档”作为上下文,它应该可以工作。

$(document).on('click','.news2clone',(function() {
// $(document) is your context and .news2clone would be the trigger inside that context.

Generally, you could use 通常,您可以使用

$('<YOUR CSS SELECTOR HERE>').click(function(){<THE CODE THAT YOU WANT TO EXECUTE HERE>}) . $('<YOUR CSS SELECTOR HERE>').click(function(){<THE CODE THAT YOU WANT TO EXECUTE HERE>})

You, you just have pass a selector that matches against any element that you want to attach this function and it is compatible with it. 您,您只是通过了一个选择器,该选择器与您要附加此功能且与之兼容的任何元素相匹配。

You can also access the item that is executing the function with $(this) . 您还可以使用$(this)访问执行功能的项目。

For example, in your div that has a class named "box-toggle2": 例如,在您的div中有一个名为“ box-toggle2”的类:

$('div.box-toggle2').click(function(){
    $(this).hide(); // JUST AS AN EXAMPLE, I'M TRYING TO HIDE THIS DIV.
});

I think that function within the click event handler will execute for all the divs that have the box-toggle2 class. 我认为click事件处理程序中的函数将对具有box-toggle2类的所有div执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM