简体   繁体   中英

Removing a specific element from a class

I have this code, it moves every element in the class "block" 10 pixels to the left. I want it to remove any of the elements which are left of 300 pixels. Why does $(this).remove() not work and what can I do to fix this?

    $(".block").animate({left:"-=10"},speed,"linear",function(){

        if(parseInt(this.style.left) < 300)
        {
            $(this).remove();
            //something
        }else{
            //something
        }
    });

html:

    <div id="container">
       <span class="block"></span>
       <span class="block"></span>
    </div>

Here's all of my code http://jsbin.com/ExET/1/

Like this? jsFiddle

$('div').on('mouseover', function() {
    $(this).animate({ 
        left: '+=10' 
    }, 200, 'linear', function() {
        if($(this).offset().left > 50) {
            $(this).remove();
        } else {
            $(this).css('background', 'blue');
        }   
    });
});

You will need to change the values but it achieves the effect you desire.

This is what you want: http://jsbin.com/olipOh/4/watch?html,css,js,output

You need to select child elements:

$(".block").find("*")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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