简体   繁体   English

遍历元素列表并隐藏它们

[英]Iterate through a list of elements and hide them

In javascript (jquery), I'm retrieving a list of elements that start with "#error-". 在javascript(jquery)中,我正在检索以“#error-”开头的元素的列表。 This works correctly. 这可以正常工作。 The problem I have is that I can't assign a value to elements of the array while looping through it. 我的问题是,在遍历数组时,无法为数组的元素分配值。

I'm using this function: 我正在使用此功能:

function HideErrorMessages(){
    var errors = $('*[id^="error-"]');
    for (var i = 0; i < errors.length; i++) {
        errors[i].css('display', none);
    }
}

As you can see, I tried this "css" possibility. 如您所见,我尝试了这种“ css”的可能性。 Doesn't work. 不起作用 I also tried: 我也尝试过:

  • errors[i].hide(); 错误[I] .hide();
  • errors[i].style.display = 'none'; errors [i] .style.display ='none';

But when using " alert(errors[i]) ", I get a response which indicates that it contains a list of "span" elements (which is correct). 但是,当使用“ alert(errors [i])”时,我得到一个响应,指示它包含“ span”元素的列表(正确)。

So how can I do to hide elements in this loop? 那么如何在此循环中隐藏元素?

Thanks! 谢谢!

Try to invoke .hide() over the jquery object, 尝试在jquery对象上调用.hide()

$('[id^="error-"]').hide();

You don't need to iterate over that elements one by one. 您无需一一遍历这些元素。 If you fetch elements from a jquery object by bracket notation, then it will return native javascript DOM node. 如果您通过括号符号从jquery对象获取元素,则它将返回本机javascript DOM节点。 So .css() will cause error as it is not a part of a DOM node. 因此.css()会导致错误,因为它不是DOM节点的一部分。

errors[i] makes reference to a property inside the jQuery object which is a selected DOM object. errors[i]引用jQuery对象内部的属性,该属性是选定的DOM对象。 There's no css function for those objects, it's a jQuery thing. 这些对象没有css函数,这是jQuery。 But you can use jQuery eq to select the object and have access to jQuery methods: 但是您可以使用jQuery eq选择对象并可以访问jQuery方法:

errors.eq(i).css('display', 'none');

You can also use each to iterate each element of the jQuery object: 您还可以使用each来迭代jQuery对象的每个元素:

$('*[id^="error-"]').each(function(){
    $(this).css('display', 'none');
});

I would go like this. 我会这样。

 $('#buttonClick').on('click', function() { var showing = $(this).closest('.thumbBrowser').find('ul li:visible'); var next = showing.last().next(); if( next.length === 0 ) { next = $(this).closest('.thumbBrowser').find('ul li').first(); } next.toggleClass('hidden').next().toggleClass('hidden'); showing.toggleClass('hidden'); }); 

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="thumbBrowser"> <ul> <li class="thumbLeft caseStudy tint tintWhite"> <a href="client-page.html"><img src="images/argus_thumb.jpg" alt="one"></a> </li> <li class="thumbRight caseStudy tint"> <img src="images/adr_thumb.jpg" alt="two"> </li> <li class="hidden thumbLeft caseStudy tint tintWhite"> <img src="images/dd_thumb.jpg" alt="three"> </li> <li class="hidden thumbRight caseStudy tint"> <img src="images/cdp_thumb.jpg" alt="four"> </li> <li class="hidden thumbRight caseStudy tint tintWhite"> <a href="client-page.html"><img src="images/pm_thumb.jpg" alt="five"></a> </li> <li class="hidden thumbLeft caseStudy tint tintWhite"> <img src="images/argus_thumb.jpg" alt="six"> </li> </ul> <div class="cycleButton" id="buttonClick"><img src="images/cycleIcon.png"></div> </div> 

 .hidden { display:none; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM