我创建了一个扩展 div,它在加载时隐藏,并使用 javascript 和 jQuery 单击时展开。我的问题是我需要在每个页面上多次使用此功能(10-30 次)。有没有办法使用数组或类似性质的东西来调用函数来处理多个 Div id(我是新手,抱歉),例如我的一些 div id 是 eb1、eb2、eb3、eb4。这是我目前适用于一个 id 的脚本:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#eb1').hide();
//hides box at the begining
jQuery("#hide").click(function() {
jQuery('#eb1').fadeOut(300);
//jQuery('#the_box').hide();
});
jQuery("#show").click(function() {
jQuery('#eb1').fadeIn(300);
//jQuery('#the_box').show();
});
});
</script>
任何帮助,甚至是解释的链接,我们将不胜感激。 谢谢, 特拉维斯
最佳答案
进一步John Conde's answer这也可以使用 attribute-starts-with:
jQuery('div[id^="eb"]').hide();
当然,仅使用特定的类名会更容易,然后选择器将变为:
jQuery('.className').hide();
引用文献:
关于javascript - 在多个 Div Id 上使用 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12498405/