admin管理员组文章数量:1389932
the code work but the console log show Cannot read property 'indexOf' of null
It cannot be seen in jsfiddle, 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();
}
});
the code work but the console log show Cannot read property 'indexOf' of null
It cannot be seen in jsfiddle, btw here is the demo of what I want.
http://jsfiddle/cw6cgg27/
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();
}
});
Share
Improve this question
asked Aug 14, 2014 at 1:17
user3836151user3836151
2313 gold badges5 silver badges11 bronze badges
2 Answers
Reset to default 2It is plaining 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.
I think changing your code into this would work:
$('br').each(function () {
console.log($(this).get(0).nextSibling.nodeValue.indexOf('作詞') > -1);
});
本文标签: javascriptCannot read property 39indexOf39 of nullStack Overflow
版权声明:本文标题:javascript - Cannot read property 'indexOf' of null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744631257a2616553.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论