小编n33*_*33x的帖子

使用递归而不是EVAL

我有一个页面中的项目列表,必须按顺序隐藏,但在上一个项目完全隐藏之后.

我做了以下代码,在那里我创建了一个大字符串,在前面的回调中插入回调,然后使用eval来执行效果,但是尽管代码工作正常,但我完全相信这不是最好的方法.这个.

// get items to hide
    var itemsToHide = jQuery(".hide");

// function responsible to hide the item
    var hideItem = function (item, callback) {
     jQuery(item).hide(100, callback)
    };

// declare an empty function, to be the first to be called
    var buff = function(){};

    for (var i = 0; i < itemsToHide.length; i++) {
// put the previous value of buff in the callback and assign this to buff
     buff = "function(){hideItem(itemsToHide[" + i + "], " + buff + …
Run Code Online (Sandbox Code Playgroud)

javascript recursion jquery eval

3
推荐指数
1
解决办法
813
查看次数

标签 统计

eval ×1

javascript ×1

jquery ×1

recursion ×1