如何使用CSS一次显示和隐藏多个元素? [英] How can I use CSS to show and hide multiple elements at once?
问题描述
我发现这篇文章 看起来像我想要的,但我似乎不能让它工作。因为它已经一年多了,我想可能有些事情可能已经改变,或者现在可能有更简单的方法。
I found this article that looked like exactly what I wanted, but I can't seem to get it to work at all. Since it is well over a year old, I thought perhaps something may have changed, or that there might be a simpler way to do it by now.
这就是说,我不能得到我上面链接的方法工作。我完全复制和粘贴,并使用< body onLoad =javascript_needed()>
,因为我不知道 $
That is to say, I cannot get the method I linked above to work. I copied and pasted exactly, and used <body onLoad="javascript_needed()">
because I wasn't sure where $(document).ready(function ()...
was supposed to go. I am, sadly, quite unfamiliar with Javascript.
推荐答案
使用这样的东西;
<script>
$(document).ready(function(){
//Code goes in here.
});
</script>
不要忘记同时从 http://jquery.com/
此外,您将要阅读选择器。
Also, you are going to want to read up on selectors.
使用 $(#myElement)
将选择ID为myElement的元素。
Using $("#myElement")
will select elements that have an id of "myElement".
使用 $(。myElement)
将选择具有类myElement的元素。
Using $(".myElement")
will select elements that have a class of "myElement".
因此;
<div class="hideMe">Content</div>
<div class="hideMe">Content</div>
<div class="hideMe">Content</div>
<div class="doNotHideMe">Content</div>
<input type="button" class="ClickMe" value="click me"/>
<script>
$(function(){
$(".ClickMe").click(function(){
$(".hideMe").hide(250);
});
});
</script>
编辑
如果要链接到jquery库在线,然后使用;
If you want to link to the jquery library online then use;
<script src="http://code.jquery.com/jquery-1.6.1.min.js" type="text/javascript"></script>
如果您下载库并将js文件插入到项目中,请使用;
If you download the library and insert the js file into your project then use;
<script src="/yourPathToTheLibrary/jquery-1.6.1.min.js" type="text/javascript"></script>
这篇关于如何使用CSS一次显示和隐藏多个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!