初学javascript问题,下一段代码怎么实现“改变紧跟着h1的元素的样式”,主要是第二个函数不懂
functionstyleHeaderSiblings(){varheaders=document.getElementsByTagName("h1");varelem;...
function styleHeaderSiblings(){
var headers=document.getElementsByTagName("h1");
var elem;
for (var i=0;i<headers.length;i++){
elem=getNextElement(headers[i].nextSibling);
elem.style.fontWeight="bold";
elem.style.fontSize="1.2em";
}
}
function getNextElement(node){
if(node.nodeType==1){
return node;
}
if(node.nextSibling){
return getNextElement(node.nextSibling);
}
return null;
}
比如说这截取部分的html代码:
<h1>Hold the front page</h1>
<p>This first xxxxxxxx xxxxx</p>
<p>This first xxxxxxxx xxxxx</p>
<p>This first xxxxxxxx xxxxx</p>
<h1>Hold page</h1>
<p>This first xxxxxxxx xxxxx</p>
<p>This first xxxxxxxx xxxxx</p> 展开
var headers=document.getElementsByTagName("h1");
var elem;
for (var i=0;i<headers.length;i++){
elem=getNextElement(headers[i].nextSibling);
elem.style.fontWeight="bold";
elem.style.fontSize="1.2em";
}
}
function getNextElement(node){
if(node.nodeType==1){
return node;
}
if(node.nextSibling){
return getNextElement(node.nextSibling);
}
return null;
}
比如说这截取部分的html代码:
<h1>Hold the front page</h1>
<p>This first xxxxxxxx xxxxx</p>
<p>This first xxxxxxxx xxxxx</p>
<p>This first xxxxxxxx xxxxx</p>
<h1>Hold page</h1>
<p>This first xxxxxxxx xxxxx</p>
<p>This first xxxxxxxx xxxxx</p> 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询