简体   繁体   中英

How to check if element being dragged has a certain class?

How can I check if a certain element being dragged has a certain class?

The JS code:

if ($('.fc-event').is('.ui-draggable')) //To check if element is being dragged
{
    console.log('This element is being dragged');
    if ($('.fc-event').is('.ui-draggable').hasClass('music')) //Can't use this.hasClass?
    {
        //place in music database table
    }
    else
    {
        //place in other database table
    }
}

The HTML code:

<div id='external-events'>
    <h4>Music</h4>
    <div class='fc-event music'>Music 1</div>
    <div class='fc-event music'>Music 2</div>
    <div class='fc-event music'>Music 3</div>
</div>

The above JS code obviously does not work, but it's the closest to the solution. (I think)

There are some events in draggable() api, there is one said drag which fires when you drag it and this callback has two arguments event, ui where ui will get you the element you are looking for.

 $( ".selector" ).draggable({ drag: function( event, ui ) { $('pre').html('$(ui.helper).hasClass("music"):'+$(ui.helper).hasClass('music')); } }); 
 .selector{width:150px; height:100px; border:solid 1px red; background:yellow;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <div class='selector music'>selector</div> <pre></pre> 

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