Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.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
Jquery 删除第一个列表项(如果它包含指定数量的项)_Jquery - Fatal编程技术网

Jquery 删除第一个列表项(如果它包含指定数量的项)

Jquery 删除第一个列表项(如果它包含指定数量的项),jquery,Jquery,我正在练习模仿bash控制台,虽然有一个问题,但实际上我已经完成了。如果元素总数超过给定值,我需要始终删除.bash行类的第一个元素 不幸的是,我为此创建了一个函数——它删除了第一个元素,但没有添加下一个元素。我能做些什么来改变这一点 函数animateConsolestring{ var bash=$”; $console-content.appendbash; var typed=new Typedbash.get0{ 字符串:[string], 打字速度:10, showCursor:f

我正在练习模仿bash控制台,虽然有一个问题,但实际上我已经完成了。如果元素总数超过给定值,我需要始终删除.bash行类的第一个元素

不幸的是,我为此创建了一个函数——它删除了第一个元素,但没有添加下一个元素。我能做些什么来改变这一点

函数animateConsolestring{ var bash=$”

; $console-content.appendbash; var typed=new Typedbash.get0{ 字符串:[string], 打字速度:10, showCursor:false, }; } 函数先删除{ 如果$'.bash行'.length==2{ $.bash-line.eachfunction{ $this.first.remove; }; } } $document.readyfunction{ setTimeoutfunction{animateConsole'//GET CONNECTION SECRET';},1000; setTimeoutfunction{animateConsole'//发送请求';deleteFirst;},2000; setTimeoutfunction{animateConsole'//等待响应';},3000; };
问题在于,您正在循环使用.bash行元素,并在每次迭代中删除第一个元素。这样做的结果是删除所有项目

要解决此问题,请不要循环:

函数animateConsolestring{ var bash=$”

; $console-content.appendbash; var typed=new Typedbash.get0{ 字符串:[string], 打字速度:10, showCursor:false, }; } 函数先删除{ 如果$'.bash行'.length==2{ $.bash行:first.remove; } } $document.readyfunction{ setTimeoutfunction{ animateConsole'//获取连接密钥'; }, 1000; setTimeoutfunction{ animateConsole'//发送请求'; 首先; }, 2000; setTimeoutfunction{ animateConsole'//等待响应'; }, 3000; };
您不需要对每个找到的元素进行foreach来删除一组jQuery元素中的第一个元素,只需执行以下操作:

$console content p.bash-line.first.remove

您还可以查看jquery first方法文档:

以下是完整的工作示例:

函数animateConsolestring{ var bash=$”

; $console-content.appendbash; 首先; var typed=new Typedbash.get0{ 字符串:[string], 打字速度:10, showCursor:false, }; } 函数先删除{ 如果$'.bash行'.length>2{ $console content p.bash-line.first.remove; } } $document.readyfunction{ setTimeoutfunction{animateConsole'//GET CONNECTION SECRET';},1000; setTimeoutfunction{animateConsole'//发送请求';},2000; setTimeoutfunction{animateConsole'//等待响应';},3000; };
你真的觉得有必要在我的答案发布20分钟后再添加一个重复的答案吗?