扫码关注官方订阅号
如题,代码如下:
1 2 3 4 5 6
类数组对象不能调用shift方法api?
shift会修改原数组,导致length属性改变,但是length是只读的。通过下面方式可以使用。
<ul class="demo"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul> </body> <script> var l = document.getElementsByClassName("demo")[0]; var a = l.children; var arry = [...a]; var r=arry.shift(); console.log(r); </script>
当然,shift是数组的方法,可以先把类数组转成数组再调用Array.prototype.slice.call(arraylike);
Array.prototype.slice.call(arraylike);
console.log(a)可以看到:__proto__:HTMLCollection HTMLCollection中并没有shift方法。
__proto__:HTMLCollection
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
shift会修改原数组,导致length属性改变,但是length是只读的。通过下面方式可以使用。
当然,shift是数组的方法,可以先把类数组转成数组再调用
Array.prototype.slice.call(arraylike);console.log(a)
可以看到:
__proto__:HTMLCollectionHTMLCollection中并没有shift方法。