简体   繁体   中英

jQuery Click element, then remove it

I'm stuck with a concept, not sure if my logic is right, it most likely isn't.

I'm hoping to achieve a div delete. So I click the div and it makes it the active div and then I can delete it with the backspace key.

So the flow is -. Click Element - Hit Backspace - $(this).remove(); , but not sure how to target the element with a click. I had:

$(".spike").live(event, function(del) {
  if (del.keyCode == 8) { $(this).remove();}
});

but it doesn't work. (The event is bound to click and ipad touch).

Basically is there any way I can use the click event to target a div , maybe to a global variable, that then allows me perform actions to it?

Well the idea is to select an item, and save the selection. Then if the key you want is pressed, delete that item.

Look at this example: http://jsfiddle.net/GVgEy/5/

change "event" for "click" which is the event you want to handle.

$(".spike").live("click", function(del) {
  if (del.keyCode == 8) { $(this).remove();}
});

Hm... I have little to offer but a demo http://jsfiddle.net/E8RaX/1

but the idea is that you assign a class to the active object and then with some prevent defaults you remove any divs with that class.

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