Reputation: 231
the code work but the console log show Cannot read property 'indexOf' of null
It cannot be seen in jsfiddle.net, btw here is the demo of what I want.
because the markup suck, so I have to find every nodevalue of br, and get rip off line that start with 作詞, 作曲, 編曲, and 監製. It work but why in the console log there is an error?
$('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();
}
});
Upvotes: 1
Views: 16122
Reputation: 27227
It is complaining that nextSibling
does not exist. You must code defensively.
$('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();
}
});
Note that my usage of Array.indexOf
exists for Internet Explorer 9+. So if you need to support IE8 you must use a polyfill or a different implementation.
Upvotes: 2
Reputation: 7288
I think changing your code into this would work:
$('br').each(function () {
console.log($(this).get(0).nextSibling.nodeValue.indexOf('作詞') > -1);
});
Upvotes: 0