简体   繁体   中英

Hide Last DIV using JavaScript?

I had a bit of a look around on Google, but couldn't find anything that worked. How do I hide the last DIV using the following code...

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

getElementsByTagName gives you an array of elements that matches the criteria. 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.

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()

它可以帮助你..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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