Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript-for循环生成事件_Javascript_Events_Function_For Loop - Fatal编程技术网

javascript-for循环生成事件

javascript-for循环生成事件,javascript,events,function,for-loop,Javascript,Events,Function,For Loop,我正在使用一个脚本,我需要使多个事件,使弹出窗口出现 我试过这个,但不起作用: for (i=0;i<=storingen;i++) { $("#storing" + i).click(function(){ centerPopup(); loadPopup(); }); } 等等 但是id为#storing(这里的数字)的div的数量是可变的,所以我想这样做,但它不起作用 我从php获得storingen变量: <script type="text

我正在使用一个脚本,我需要使多个事件,使弹出窗口出现

我试过这个,但不起作用:

for (i=0;i<=storingen;i++)
    {
        $("#storing" + i).click(function(){ centerPopup(); loadPopup(); });
    }
等等

但是id为#storing(这里的数字)的div的数量是可变的,所以我想这样做,但它不起作用

我从php获得storingen变量:

<script type="text/javascript">aantalstoringen('.$aantalstoringen.')</script>
我做了一个警报(storingen),追踪到了正确的号码,所以没问题

可能是for循环不起作用,因为它不在aantalstoringen函数中,而是在另一个函数中:

$(document).ready(function() {
我使用本教程制作javascript: 您得到的脚本是: 请改用选择器:

$('[id^="storing"]').click(function(){ ... });
基本上,它的意思是“查找ID以“存储”开头的所有元素。”

如果您需要更明确的信息,可以在内部测试
id
,以应用更好的过滤。e、 g

$('[id^="storing"]')
  // make sure all IDs end in a number
  .each(function(i,e){
    if (/\d$/.test(e.id)) {
      // now that we only have ids that begin with storing and end in
      // a number, bind the click event
      $(e).click(function(e){ ... });
    }
  });

它可以是任何数量的东西。如果您向我们展示更多的代码,比如所有的
aantalstoringen
函数,将会有所帮助

以下方面应起作用:

function aantalstoringen(storingen) {
    $(document).ready(function() {
        for (i=0;i<=storingen;i++) {
            $("#storing" + i).click(function(){ centerPopup(); loadPopup(); });
        }
    });
}

首先给div起一个类名,比如class=“popupDiv”。那你试试这样的

$('.popupDiv').live('click', function(){
      // var id = $(this).attr('id'); By doing so you will get the current id of the div too.
      centerPopup();
      loadPopup(); 
});

正如Brad正确地说的那样,您不必知道这些元素的数量,您只需迭代所有具有
id
的元素即可

所以你的代码应该是:

$(document).ready(function() {
    $('[id^="storing"]').click(function() {
        centerPopup();
        loadPopup();
    });
});

您不会创建几十个调用同一处理程序的事件侦听器。在DOM的更高级别上创建一个侦听器,并使其仅在目标的ID与模式匹配时才作出反应


这就是为什么像jQuery这样的LIB会教孩子们不礼貌…--

我建议您将所有这些元素赋予相同的类,只需执行
$('.classname')。另外,我想指出,这个函数是用来显示特定的div的[就像你在链接的popup.js文件中看到的那样]。每个div都有一个不同的ID,因为内容不同,所以我不能使用搜索每个ID存储的元素的函数*如果我不是很清楚,我很抱歉。我不确定你想通过映射实现什么。。。您将得到一个字符串数组,无法将事件处理程序绑定到它们。@FelixKling:True,仍在唤醒中。应该把逻辑放在click语句或each语句中,在这种情况下,我将使用
.filter
代替;)不用担心,早上好:)我向您展示了所有的aantalstoringen函数,它只是导入由PHP跟踪的变量的那一行。是的,您知道,我意识到我使用的脚本在这里完全不方便使用,因为它们都调用相同的处理程序。。。我必须制作不同的EventListener来调用不同的处理程序来实现这一点。。。我真蠢。。
$(document).ready(function() {
    $(".storing").click(function(){ centerPopup(); loadPopup(); });
});
$('.popupDiv').live('click', function(){
      // var id = $(this).attr('id'); By doing so you will get the current id of the div too.
      centerPopup();
      loadPopup(); 
});
$(document).ready(function() {
    $('[id^="storing"]').click(function() {
        centerPopup();
        loadPopup();
    });
});