Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts

    live() instead in jquery 1.26?

    Code:
    $('#content').find('p:eq(0)').append('<span class="more"> | more</span><span class="less"> | less</span>')
    $('p:gt(0)').hide();
    
    $('.more, .less').live('click', function() {
        $('#content').find('p:gt(0)').slideToggle();
        $('.more, .less').toggle();
    })
    i using drupal-6,the jquery is 1.26. if i don't want to upgrade the jquery. is there a way to get the effect?

    jquery is 1.26 haven't have a suppport to the live() function. is there another function can instead of it. or alter the code to get the effect?


    some one told me to dd a normal click handler to .content, then check whether $(e.target).is('.more, .less').


    i alter the code to this
    Code:
     $('#content').find('p:eq(0)').append('<span class="more"> | more</span><span class="less"> | less</span>')
     $('p:gt(0)').hide(); 
    $('#content').click(function(){
     if($(e.target).is('.more, .less'))
    {
     $('#content').find('p:gt(0)').slideToggle(); $('.more, .less').toggle(); 
    } 
    })
    but it can't work.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    The code is missing the "e" parameter ... it should work then
    Code:
    $('#content').click(function(e){
       if($(e.target).is('.more, .less')) {
          $('#content').find('p:gt(0)').slideToggle();
          $('.more, .less').toggle(); 
       } 
    });

  • Users who have thanked devnull69 for this post:

    runeveryday (04-12-2011)

  • #3
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    The code is missing the "e" parameter ... it should work then
    Code:
    $('#content').click(function(e){
       if($(e.target).is('.more, .less')) {
          $('#content').find('p:gt(0)').slideToggle();
          $('.more, .less').toggle(); 
       } 
    });
    you're my hero! could you explain what's the function meaning?

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Instead of binding a click handler to the (yet non existing) classes more/less you bind a click handler to the container #content. Each element inside this container propagates ("bubbles") its click events up in the DOM tree to the next higher level elements (including #content). The click handler there can check which element originally received the click event using e.target

    .is() can tell you which class(es) the clicked element belongs to ... and the rest is your original code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •