javascript - 在多个 Div Id 上使用 javascript

标签 javascript jquery css

我创建了一个扩展 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();

JS Fiddle demo .

当然,仅使用特定的类名会更容易,然后选择器将变为:

jQuery('.className').hide();

JS Fiddle demo .

引用文献:

关于javascript - 在多个 Div Id 上使用 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12498405/

相关文章:

css - 背景元素可通过 IE9 中的绝对定位链接选择

javascript - 无法读取表行未定义的属性 'style'

javascript - 如何使用 React 从数据库中获取正确的数据

jquery 事件触发两次

javascript - 当其他 DIV 悬停时,jQuery 显示 DIV,不起作用

javascript - Tablesorter,分组排序

javascript - 使用显示 : table and margin: auto, 时图标居中对齐,但当我更改为显示 :none it does not align to center after . show()

javascript - 谷歌地图 v3 : adding a new map when function 'failure' called

javascript - 如何通过id查找&lt;script&gt;

javascript - 从 ionic2 中的 javascript 访问 typescript 方法