此处是html(在多个位置出现,但每种外观应区别对待):
<div class="foot-nav-heading">Click me!</div>
<div class="textwidget">
<ul class="quick-link accordion-content">
<li>Test</li>
</ul>
</div>
这是JavaScript代码:
var accordions = document.getElementsByClassName("foot-nav-heading");
for (var i = 0; i < accordions.length; i++) {
accordions[i].onclick = function() {
this.classList.toggle('is-open');
var content = this.nextElementSibling;
if (content.style.maxHeight) {
// accordion is currently open, so close it
content.style.maxHeight = null;
} else {
// accordion is currently closed, so open it
content.style.maxHeight = content.scrollHeight + "px";
}
}
}
我要定位“ div.textwidget”元素中的“ ul”。我已经尝试过
var content = this.next.(".text-widget").find(".accordion-content")
但是它不起作用。
谢谢!
答案 0 :(得分:1)
您的代码有效!问题不在于同级,而是不能将css height设置为null
var accordions = document.getElementsByClassName("foot-nav-heading");
for (var i = 0; i < accordions.length; i++) {
accordions[i].onclick = function() {
this.classList.toggle('is-open');
var content = this.nextElementSibling;
if (content.style.maxHeight != "0px") {
// accordion is currently open, so close it
content.style.maxHeight = "0px";
} else {
// accordion is currently closed, so open it
content.style.maxHeight = "200px";
}
}
}
<div class="foot-nav-heading">Click me!</div>
<div class="textwidget">
<ul class="quick-link accordion-content">
<li>Test</li>
</ul>
</div>
Here is the javascript code:
<div class="foot-nav-heading">Click me!</div>
<div class="textwidget">
<ul class="quick-link accordion-content">
<li>Test</li>
</ul>
</div>
Here is the javascript code:
编辑:
您尝试使用的代码是获取nextSibling。但是您可能会有带有回车符的多余textnode尝试以下代码段:
document.querySelectorAll(".textwidget")[0].childNodes.forEach(function(el){
console.log(el.nodeName,el.nodeType);
});
<div class="foot-nav-heading">Click me!</div>
<div class="textwidget">
<ul class="quick-link accordion-content">
<li>Test</li>
</ul>
</div>
因此尝试直接定位是有风险的(在您的示例中,您可以使用content.childNodes[1]
来获得所需的内容,但是如果您不知道html的编写方式,则很危险。可能是content.childNodes[0]
没有回车符)。因此,最好的选择是解析childNodes并在它们完全满足条件时在数组中设置变量或push元素。在您的情况下:
var ul;
content.forEach(function(el){
if (el.nodeName === 'UL') ul = el;
}
或从节点列表中创建一个数组以过滤所需的节点:
var ul = Array.from(document.querySelectorAll(".textwidget")[0].childNodes).filter(function(el) {if(el.nodeName == 'UL') return el});
// or use es6 features
// var ul = [...document.querySelectorAll(".textwidget")[0].childNodes].filter(e => e.nodeName == 'UL');
console.log(ul[0]);
<div class="foot-nav-heading">Click me!</div>
<div class="textwidget">
<ul class="quick-link accordion-content">
<li>Test</li>
</ul>
</div>
答案 1 :(得分:0)
main#main
)e.target
,您可以准确确定点击了哪个标签。关于OP的目标:
“我想在不使用类或ID的情况下将下一个兄弟对象作为子元素。”
考虑到实际布局,每个“按钮”都可以针对nextElementSibling
添加/删除类,并且仍然可以使用CSS选择器影响nextElementSibling
的子级。简而言之,将嵌套在父对象中的标签定位到更易于访问的标签上又浪费了时间和精力,反过来又使得通过CSS访问其后代变得更容易。
<button>CLICK</button>
<section class='accordion'>
<ul class='list'>
<li>ITEM</li>
</ul>
</section>
/* By default this hides .list */
.accordion {
max-height: 0;
opacity: 0;
font-size: 0;
transition: all 0.3s;
border: 3px outset #666;
}
/* This class is added/removed when <button> is clicked */
/* .open is nextElementSibling and anytime .open is added, it's child .list
benefits from it as well
*/
.open,
.open .list {
height: auto;
max-height: 1000px;
opacity: 1;
font-size: 16px;
transition: all 0.3s;
border: 3px outset #666;
}
演示中评论的详细信息
// Reference the parent tag (main#main)
var main = document.getElementById('main');
/*
Register <main> to the click event...
toggleText() is the callback function
*/
main.addEventListener('click', toggleText);
// Pass the Event Object (e)
function toggleText(e) {
// Reference e.target -- clicked tag (header.head)
var tgt = e.target;
/*
Reference the next adjacent sibling of e.target (article.text)
*/
var actTxt = tgt.nextElementSibling;
// Collect all article.text into a NodeList
var txts = document.querySelectorAll('.text');
// if clicked tag has class .head...
if (tgt.classList.contains('head')) {
/*
On each loop...
if the current article.text is NOT e.target's .text sibling...
...remove class .open.
Toggle .open class on e.target's .text sibling.
*/
for (let i = 0; i < txts.length; i++) {
if (txts[i] !== actTxt) {
txts[i].classList.remove('open');
}
}
actTxt.classList.toggle('open');
}
}
.head {
cursor: pointer;
border: 3px outset #666;
text-align:center;
padding: 3px 5px;
}
.text {
max-height: 0;
opacity: 0;
font-size: 0;
transition: all 0.3s;
border: 3px outset #666;
}
.open,
.open .menu {
height: auto;
max-height: 1000px;
opacity: 1;
font-size: 16px;
transition: all 0.3s;
border: 3px outset #666;
}
<main id='main'>
<header class="head">MENU</header>
<article class="text">
<ul class="menu">
<li>ITEM 1</li>
<li>ITEM 2</li>
<li>ITEM 3</li>
</ul>
</article>
<header class="head">MENU</header>
<article class="text">
<ul class="menu">
<li>ITEM 1</li>
<li>ITEM 2</li>
<li>ITEM 3</li>
</ul>
</article>
<header class="head">MENU</header>
<article class="text">
<ul class="menu">
<li>ITEM 1</li>
<li>ITEM 2</li>
<li>ITEM 3</li>
</ul>
</article>
</main>