Javascript中的current和next函数在php中是等价的
javascript what is the equivalent of current and next function in php?
我想在Javascript中获得当前项目,并在其中处理后移动数组指针
就像php函数一样,current()
&next()
比如
array.current()
array.next()
制作原型函数。例如
Array.prototype.cursorPosition = 0;
Array.prototype.current = function(){
return this[this.cursorPosition];
}
Array.prototype.next = function(){
this.cursorPosition=this.cursorPosition+1;
return this[this.cursorPosition];
}
Array.prototype.previous = function(){
this.cursorPosition=this.cursorPosition-1;
return this[this.cursorPosition];
}
// implementation
var fruits = ["Banana", "Orange", "Apple", "Mango"];
alert(fruits.current()) ;
alert(fruits.next()) ;
alert(fruits.previous()) ;
for (var i in myArray){
doSomething();
}
我不认为有一个你必须自己做一个函数。比如
function nextItem(num) {
return p[($.inArray(num, p) + 1) % p.length];
}
但是我不明白为什么你想要这样的东西当你可以迭代数组的时候
虽然没有经过修饰,但这将是一个良好的开端。
var arr = [1,2,3,4];
Array.prototype.currentIndex = -1;
Array.prototype.next = function(){
if(this.currentIndex + 1 < this.length){
this.currentIndex += 1;
return this[this.currentIndex];
}else{
return;
}
};
Array.prototype.current = function(){
return this[this.currentIndex];
};
演示相关文章:
- 使用php或javascript从facebook相册URL中删除多余的部分
- 将PHP变量传递给jQuery时遇到问题
- 通过javascript重定向html传递php变量
- 将数组从PHP传递到Javascript
- 如何在php文件中获取$.post-ajax传递的值
- Javascript运行php文件,然后下载文件
- PHP中的setcookie仅适用于localhost
- 如何使用jquery处理php循环通过元素
- 通过Ajax将JavaScript函数传递给PHP文件
- 而循环只设置php中输入字段中的第一个值
- 我需要从php调用javascript或jquery
- 如何通过php页面将数据库值检索到jquery自动完成框中
- PHP AJAX图片上传示例不上传
- 通过php页面中的js强制下载txt
- 如何在PHP中使用$_POST获取Select元素值
- 我想使用模态通过php文件发送邮件,并且我希望在提交关闭后关闭pop
- 如何在php变量中嵌入JQuery代码
- 在PHP中使用javascript更改页面标题'if'
- 如何将字符串值从php页面发送到java脚本页面
- Javascript中的current和next函数在php中是等价的