如何使用javascript同时显示/隐藏多个元素 [英] How to show / hide multiple elements at the same time with javascript
问题描述
所以,我有我的头标签之间
< script type =text / javascript>
hidden_links = document.getElementsByName(javascript_needed);
for(i = 0; i< hidden_links.length; i ++){
hidden_links [i] .style.display =visible;
}
< / script>
我的div都类似于
< div name =javascript_neededstyle =display:none;> stuff< / div>
这里的总目标是在禁用javascript时让这些div隐藏,并重新启用当javascript被启用....但无论什么原因,我的代码不工作。我曾经试过在webkit控制台,没有什么errored = \
JavaScript是在divs之前DOM。在DOM准备好之后做标准的方法是使用jQuery的 $(document).ready(function(){});
,但还有其他方法
旧的方式是使用< body onload =myfunction()>
p>
这里是一个较新的方法( edit: put display:none
into CSS) p>
HTML:
< p class ='javascript_needed'> hello< ; / p>
CSS:
.javascript_needed {display:none;}
JavaScript: / p>
$(document).ready(function(){
$('。javascript_needed')。show ;
});
So, I have this between my head tags
<script type="text/javascript">
hidden_links = document.getElementsByName("javascript_needed");
for (i = 0; i < hidden_links.length; i++) {
hidden_links[i].style.display = "visible";
}
</script>
And my divs are all similar to
<div name="javascript_needed" style="display: none;">stuff</div>
the overall goal here, is to have these divs hide when javascript is disabled, and re-enable them when javascript is enabled.... but for whatever reason, my code doesn't work. I ever tried it in the webkit console, and nothing errored =\
The JavaScript is executed before the divs are in the DOM. The standard way to do something after the DOM is ready is to use jQuery's $(document).ready(function () { });
, but there are other ways as well.
The oldschool way is to use <body onload="myfunction()">
.
Here's a newer way (edit: put display:none
into CSS):
HTML:
<p class='javascript_needed'>hello</p>
CSS:
.javascript_needed {display:none;}
JavaScript:
$(document).ready(function () {
$('.javascript_needed').show();
});
这篇关于如何使用javascript同时显示/隐藏多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!