简体   繁体   English

使用JavaScript隐藏上一个DIV?

[英]Hide Last DIV using JavaScript?

I had a bit of a look around on Google, but couldn't find anything that worked. 我在Google上浏览了一下,但找不到任何有效的方法。 How do I hide the last DIV using the following code... 如何使用以下代码隐藏上一个DIV ...

document.getElementsByTagName('div').style.display = 'none';

getElementsByTagName gives you an array of elements that matches the criteria. getElementsByTagName为您提供匹配条件的元素数组。 So you need to specify the element index. 因此,您需要指定元素索引。 So you have to find the size of array and apply the style to size-1 th element. 因此,您必须找到数组的大小并将样式应用于第size-1size-1元素。

var el = document.getElementsByTagName('div');
var size = el.length;
el[size-1].style.display = 'none';

UPDATE: 更新:

here is a demo fiddle 这是一个演示小提琴

Just retrieve the length and take the last one of the array (length minus one, as it is zero based): 只需获取长度并获取数组的最后一个(长度减去一,因为它是从零开始的):

var divs = document.getElementsByTagName('div');
divs[divs.length - 1].style.display = 'none';

使用JQuery的last()方法( http://api.jquery.com/last/

$('div').last().hide();
$("div:last").hide()

它可以帮助你..

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

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