Почему не работает remove(); в AJAX
У меня есть несколько <iframe>, я хочу каждый проверить статус если iframe умерла ссылка (не рабочая ссылка) - то удалить Iframe со страницы. Я могу получить alert(), все работает. Но я не могу послать команду удалить данный iframe. Что здесь не так?
$('iframe').each(function() {
var urlExists = function(url, callback) {
if ( ! $.isFunction(callback)) {
throw Error('Not a valid callback');
}
$.ajax({
type: 'HEAD',
url: url,
success: $.proxy(callback, this, true),
error: $.proxy(callback, this, false)
});
};
urlExists( $(this).attr('src') , function(success) {
if (success) {
//alert('Yay!');
} else {
alert('Oh no! Oh no! Oh no! Oh no! Oh no! Oh no!');
$(this).remove();
}
});
});
<iframe width="1762" height="251" src="https://videofile.online/embed/620897/" frameborder="0" allowfullscreen></iframe>
<br>
<iframe width="1762" height="251" src="https://videofile.online/embed/620897/" frameborder="0" allowfullscreen></iframe>
<br>
<iframe width="1762" height="251" src="https://vide2ofile.online/embed/620897/" frameborder="0" allowfullscreen></iframe>
Ответы (1 шт):
Автор решения: InDevX
→ Ссылка
У вас this не указывает на iframe т.к. находится в другом контексте, грубо говоря.
Замените $('iframe').each(function() { на $('iframe').each(function(index, item) {
И удаление $(item).remove();