意外落入if语句
unexpected fall to if statement
HTML
<div id="main">
<span>v</span>
<div id="main_cursor"></div>
</div>
Javascript
function highlight_word() {
var cursor = document.getElementById(area + '_cursor'),
pe = cursor.previousElementSibling,
ne = cursor.nextElementSibling;
var word = [];
f();
word = word.join();
if (isInArr(keywords_arr, word)) {
f(true);
};
function f(p = false) {
while ((pe === null ? " " : pe.innerHTML) !== " " ||
(ne === null ? " " : ne.innerHTML) !== " ") {
if (pe !== null) {
while (pe.innerHTML !== " ") {
p === true ? pe.style.color = "green" : word.push(pe.innerHTML);
pe = pe.previousElementSibling;
}; // end of while
word = word.reverse();
}; //end of if
if (ne !== null) {
while (ne.innerHTML !== " ") {
p === true ? pe.style.color = "green" : word.push(ne.innerHTML);
ne = ne.nextElementSibling;
}; //end of while
}; //end of if
}; // end of while
}; // end of function f
};
目标
目标是首先获取位于main_cursor
和具有空格作为内部文本的跨度之间的所有跨度的内部文本,并检查单词获取是否存在于数组keywords_arr
中。该单词是否存在于该数组中,然后更改文本的颜色所有这些跨度的
或者如果单词在keyword_arr
中,则仅突出显示
错误
未捕获的类型错误:无法读取空的属性"innerHTML"
错误显示在行中
while(pe.innerHTML!==" "){
并且只有当CCD_ 4的条件得到满足时才会发生这种情况,这是不应该发生的!
我该怎么办?
此循环内部
while (pe.innerHTML !== " ") {
p === true ? pe.style.color = "green" : word.push(pe.innerHTML);
pe = pe.previousElementSibling;
}; // end of while
您正在重新分配pe
的值。如果pe
不再有任何以前的同级,它将被分配给null
。
相反,您可以将条件更改为这样,以确保pe
在检查其innerHTML
:之前有效
while (pe && pe.innerHTML !== " ") {
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何在ReactJS JSX中执行嵌套的if-else语句
- 如何在 API 调用后和 if 语句中启用提交按钮
- 带有多个elseif的Javascript if语句
- Sharepoint JScript if语句未执行
- Javascript:If-then语句在函数中不起作用
- for循环中的javascript if语句找不到==
- jquery if语句返回return wong语句
- 在if语句下的html中使用javascript变量
- 将错误与if语句混淆
- 如何检查if/else语句中的随机条件
- 循环通过数组的If语句不起作用
- jQuery模板中的If语句
- 如何从数据库中回显If语句
- 如何减少if语句的数量
- Node.js-承诺和锥形语句(if、switch等)-如何构建
- 如何在嵌套ng重复指令中使用控制语句(if语句)
- 如何创建语句if包含"&"
- If语句- If主体有一个类remove在滚动原生javascript
- Javascript中的if语句- if - else问题