Javascript 为什么我的firstNode.nodeValue返回null?
我试图将第一个p标记拆分为3行,并使用javascript节点在第二个p标记中显示它们。但我得到的返回值是null Chrome向我抛出一个运行时错误,说它“无法读取null的属性”nodeValue” 有人能帮我吗Javascript 为什么我的firstNode.nodeValue返回null?,javascript,nodes,Javascript,Nodes,我试图将第一个p标记拆分为3行,并使用javascript节点在第二个p标记中显示它们。但我得到的返回值是null Chrome向我抛出一个运行时错误,说它“无法读取null的属性”nodeValue” 有人能帮我吗 文件 隐源性{ 显示:无; } 嗨,我叫约翰·多伊 我正在学习网络开发 我已经100岁了。 常量breakLineFunction=()=>{ const span=document.querySelector(“#单行”); document.querySelector
文件
隐源性{
显示:无;
}
嗨,我叫约翰·多伊
我正在学习网络开发
我已经100岁了。
常量breakLineFunction=()=>{
const span=document.querySelector(“#单行”);
document.querySelector(“#oneLine”).setAttribute(“class”,“hideOriginal”)
让newLine=“”;
const first=span.firstChild.firstChild.nodeValue;
const second=spans.firstChild.nextElementSibling.firstChild.nodeValue;
const last=span.lastChild.firstChild.nodeValue;
常量brk=“
”;
换行符=`${first}${brk}${second}${brk}${last}`;
document.querySelector(“.lineBreak”).innerHTML=newLine;
}
document.addEventListener(“DOMContentLoaded”,()=>{
document.querySelector(“fixLineBreak”).addEventListener(“单击”,breakLineFunction);
})
const breakLineFunction=()=>{
const span=document.querySelector(“#单行”);
document.querySelector(“#oneLine”).setAttribute(“class”,“hideOriginal”)
让newLine=“”;
const first=span.firstChild.nodeValue;
const second=spans.firstChild.nextElementSibling.firstChild.nodeValue;
const last=span.lastElementChild.textContent
;
常量brk=“
”;
换行符=`${first}${brk}${second}${brk}${last}`;
document.querySelector(“.lineBreak”).innerHTML=newLine;
}
document.addEventListener(“DOMContentLoaded”,()=>{
document.querySelector(“fixLineBreak”).addEventListener(“单击”,breakLineFunction);
})
文件
隐源性{
显示:无;
}
嗨,我叫约翰·多伊
我正在学习网络开发
我已经100岁了。
const breakLineFunction=()=>{
const span=document.querySelector(“#单行”);
document.querySelector(“#oneLine”).setAttribute(“class”,“hideOriginal”)
让newLine=“”;
const first=span.firstChild.nodeValue;
const second=spans.firstChild.nextElementSibling.firstChild.nodeValue;
const last=span.lastElementChild.textContent
;
常量brk=“
”;
换行符=`${first}${brk}${second}${brk}${last}`;
document.querySelector(“.lineBreak”).innerHTML=newLine;
}
document.addEventListener(“DOMContentLoaded”,()=>{
document.querySelector(“fixLineBreak”).addEventListener(“单击”,breakLineFunction);
})
文件
隐源性{
显示:无;
}
嗨,我叫约翰·多伊
我正在学习网络开发
我已经100岁了。
u确定吗?同样的错误突然出现up@TheBombSquadthanksit现在工作@Schnitzler检查:D@TheBombSquad谢谢,它现在运行时没有语法错误,但是,出于某种原因,浏览器将第一个span元素分配给“second”常量,第二个span元素没有分配到任何位置,“first”常量的值为“”.@Schnitzler给点时间来理解密码你确定吗?同样的错误突然出现up@TheBombSquadthanksit现在工作@Schnitzler检查:D@TheBombSquad谢谢,它现在运行时没有语法错误,但是,出于某种原因,浏览器将第一个span元素分配给“second”常量,第二个span元素没有分配到任何位置,“first”常量的值为“
”@Schnitzler给点时间来理解代码最后一个应该是什么?这就是错误来自于
last
应该是什么?这就是错误的来源