Kod działa, ale kłoda konsoli Pokaż nie może odczytać właściwości "indexof" z NULL
Nie można zobaczyć w Jsfiddle.net, BTW tutaj jest demo tego, czego chcę.
Ponieważ markup jest ssać, więc muszę znaleźć każdą nogę BR i zdobądź RIP Off Linii, która zaczyna się od 作詞, 作曲, 編曲 i 監製. Działa, ale dlaczego w dzienniku konsoli jest błąd?
$('br').each(function () {
if ((this.nextSibling.nodeValue.indexOf('作詞') > -1) || (this.nextSibling.nodeValue.indexOf('作曲') > -1) || (this.nextSibling.nodeValue.indexOf('編曲') > -1) || (this.nextSibling.nodeValue.indexOf('監製') > -1)) {
$(this.nextSibling).remove();
$(this).remove();
}
});
1
user3836151
14 sierpień 2014, 05:17
2 odpowiedzi
Najlepsza odpowiedź
Narzeka się, że nextSibling
nie istnieje. Musisz zostać defensywnie.
$('br').each(function () {
if (!this.nextSibling) {
return;
}
var nodeValue = this.nextSibling.nodeValue.trim();
var invalid = ['', '作詞', '作曲', '編曲', '監製'];
if (invalid.indexOf(nodeValue) !== -1) {
$(this.nextSibling).remove();
$(this).remove();
}
});
Zauważ, że moje użycie Array.indexOf
istnieje dla Internet Explorer 9+. Więc jeśli musisz wspierać IE8, musisz użyć posiłku lub innej implementacji.
2
Frambot
15 sierpień 2014, 16:34
Myślę, że zmieniając się do tego kodeksu:
$('br').each(function () {
console.log($(this).get(0).nextSibling.nodeValue.indexOf('作詞') > -1);
});
0
Bla...
14 sierpień 2014, 02:25