JQuery -循环通过一个数组UI对话框方法
JS - looping through an array with JQuery UI dialog method
我有以下jQuery代码使用jQuery UI
编辑:$(document).ready(function(){
$('#the-link-2').click(function(){
var array = ["test1","test2","test3","test4"]
var i = 1;
while (i<array.length) {
var newDiv = $(document.createElement('div'));
$(newDiv).html(array[i]).dialog({
buttons: {
'Previous': function() {
$(this).dialog("close");
i--;
},
'Next': function() {
$(this).dialog("close");
i++;
}
}
}
});
});
});
我试图循环遍历数组的项(从item# 1开始)。对话框显示项目,并根据用户单击的按钮移动到上一个/下一个项目。它不起作用(没有任何东西被解雇)。如果我删除"while"循环,我确实得到了包含#1项的对话框。谁能给我正确的语法,以获得所需的结果,请?谢谢。
我为此创建了一个小提琴并更新了您的脚本。与其循环它们,不如创建一个你想要的递归函数:
脚本:
var array = ["test1", "test2", "test3", "test4"];
$('#the-link-2').click(function() {
var current = 0; // current data to show
createDialog(array[current], current); // create the dialog for current data
});
function createDialog(data, current) {
var $div = $('<div>'); // create a new div element
// add the html content of new div by passing array[current]
// and create the corresponding dialog
$div.html(data).dialog({
buttons: {
'Previous': function() {
if (current == 0) {
// do nothing if no more prev data
// or you can add some extra function here
// like close the dialog if no more prev data
return;
} else {
current--;
}
$(this).dialog("close").remove(); // close the dialog and remove the div
createDialog(array[current], current); // call createDialog again passing new current data
},
'Next': function() {
if (current == (array.length - 1)) {
// do nothing if no more next data
// or you can add some extra function here
// like close the dialog if no more next data
return;
} else {
current++; // increment current to next data
}
$(this).dialog("close").remove(); // close the dialog and remove the div
createDialog(array[current], current); // call createDialog again passing new current data
}
}
});
}
FIDDLE: HERE
相关文章:
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 根据id将json数组组合为一个json数组
- JavaScript数组包含一个值
- 对一个对象使用reduce可以返回一个没有't在数组中包含目标字母
- jQuery$.inArray()总是返回-1和一个对象数组
- 在数组中的一个元素上设置多个值
- javascript处理一个对象数组以获得一个新的对象数组
- 作为一个二维数组,从ajax接收
- 你能用来自数组的属性名称生成一个对象吗
- 多维关联数组的最后一个索引
- 如何创建一个方法来验证数组的范围
- 循环以检查数组中的最后一个图像
- 在Javascript中将一个值和字符串数组转换为if语句
- 算法:从数组(javascript/angular)中按当前日期获取上一个和下一个事件
- 如何将一个对象添加到每个对象数组中
- 如何创建一个谷歌地图地理坐标数组
- 如何从另一个带下划线的数组中筛选带元素的数组
- 如何在PHP数组中加载下一个youtube
- 查找对象数组是否包含其中一个标记的最快方法
- 如何使用Javascript和Ajax传递一个HTML选择标签到PHP $_POST与一个(数组)var当名称属性是一个