gpt4 book ai didi

javascript - 为什么打开页面后无法隐藏内容?

转载 作者:行者123 更新时间:2023-11-28 09:17:17 24 4
gpt4 key购买 nike

我已将 Prototype 库添加到我的网站,然后添加以下代码。当我打开页面时,我希望隐藏 ul 中的所有内容。

 Event.observe(window, 'load', function() {
$$('.block-category li.parent ul').hide() //why this line doesn't work
$$('.block-category li.parent > a span').each(function (element) {
element.observe('click', function (e) {
e.element().up().next('ul').toggle();
e.preventDefault();
});
});
});

html:

    <div class="block block-category">
<li class="level-top parent">
<a href="example.com/...."><span>text one</span></a>
<ul> //the 1 ul
<li><a><span>....</span></a></li>
<li><a><span>....</span></a></li>
<li><a><span>....</span></a></li>
<li><a><span>....</span></a></li>
</ul>
</li>

<li class="level-top"><a href="..."><span>....</span></a></li>


<li class="level-top parent">
<a href="example.com/...."><span>text two</span></a>
<ul> //the 2 ul
<li><a><span>....</span></a></li>

</ul>
</li>
<li class="level-top parent">
<a href="example.com/...."><span>text three</span></a>
<ul>
<li><a><span>....</span></a></li>
<li><a><span>....</span></a></li>
</ul>
</li>
</div>

谢谢。

最佳答案

$$() 方法返回与 CSS 选择器匹配的元素列表 - 如果您想迭代它们并运行相同的方法,请使用 invoke() 方法

$$('.block-category li.parent ul').invoke('hide');
$$('.block-category li.parent ul').invoke('observe','click',function(){
alert('element was clicked');
});

如果您需要做的不仅仅是运行单个方法,请使用 each() 方法

$$('.block-category li.parent ul').each(function(item){
item.hide();
//item.update('new content');
//etc
});

关于javascript - 为什么打开页面后无法隐藏内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15502121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com