简体   繁体   中英

javascript (safari) - 'undefined' is not a function evaluating elm.parentNode.remove(elm)

So this little chunk of js works fine in every other browser except safari. Its really simple. Heres my HTML:

<div>
<input type="text" class="input_l" name="location[]" />
<a onclick="closeLocation(this)">Remove</a>
</div>

the js is:

function closeLocation(elm){
    elm.parentNode.remove(elm);
}

basically when I click 'remove' I want its parent div removed with everything in it. AGAIN it works fine in every browser except safari here is the error:

'undefined' is not a function (evaluating 'elm.parentNode.remove(elm)')

Im not sure whats going on, thanks in advance.

You need to use Node.removeChild

 elm.parentNode.removeChild(elm);

when I click 'remove' I want its parent div removed with everything in it

As per your requirement you need to use

 elm.parentNode.parentNode.removeChild(elm.parentNode);

尝试

elm.parentNode.parentNode.removeChild(elm.parentNode);

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