【发布时间】:2011-10-16 15:40:17
【问题描述】:
我正在使用一个脚本,我需要在其中创建多个事件以显示弹出窗口。
我试过了,但它不起作用:
for (i=0;i<=storingen;i++)
{
$("#storing" + i).click(function(){ centerPopup(); loadPopup(); });
}
输出应该是:
$("#storing0").click(function(){ centerPopup(); loadPopup(); });
$("#storing1").click(function(){ centerPopup(); loadPopup(); });
$("#storing2").click(function(){ centerPopup(); loadPopup(); });
$("#storing3").click(function(){ centerPopup(); loadPopup(); });
等等
但是id为#storing(number here)的div的数量是可变的,所以我想做这个,但它不起作用......
我从 php 获取 storageen 变量:
<script type="text/javascript">aantalstoringen('.$aantalstoringen.')</script>
我在 js 文件中像这样选择:
function aantalstoringen(storingen){
storingen=storingen;
}
我做了一个alert(storingen),它追踪到了正确的数字,所以没关系。
可能是 for 循环不起作用,因为它不在 anantalstoringen 函数中,而是在另一个函数中:
$(document).ready(function() {
我使用本教程制作了 javascript: http://yensdesign.com/2008/09/how-to-create-a-stunning-and-smooth-popup-using-jquery/#popup1 你得到的脚本是这样的: http://yensdesign.com/tutorials/popupjquery/popup.js
【问题讨论】:
-
我建议你给所有这些元素一个相同的类,然后
$('.classname').click(function(){...});。 -
另外,我想指出这个函数是为了显示特定的 div [就像你可以在链接的 popup.js 文件中看到的那样]。每个 div 都有不同的 ID,因为内容不同,所以我不能使用该函数来搜索具有 ID 存储的每个元素* 如果我不是很清楚,我很抱歉。
标签: javascript events function for-loop