简体   繁体   English

元素的 -1 索引是什么意思?

[英]What does -1 index of an element mean?

I am trying to debug the jquery/js code below.我正在尝试调试下面的 jquery/js 代码。 So I was looking for the index of the elements.所以我在寻找元素的索引。 However it's returning -1 .但是它返回-1

In jsFiddle it is working well.jsFiddle 中它运行良好。 But when migrating to asp.net IDE, this is giving -1 .但是当迁移到 asp.net IDE 时,这是给-1

The markup:标记:

<div class="cart">
   <h2>Cart 1</h2>
   <p>Books </p> 
     <table class="cartcontent" data-options="fitColumns:true, singleSelect: true">                                
         <thead>
           <tr>
               <th data-options="field:'name',width:100">Name</th>
               <th data-options="field:'quantity',width:100">Quantity</th>
               <th data-options="field:'balance',width:100,align:'right'">Balance</th>
               <th data-options="field:'remove',width:100,align:'right'">Remove</th>
            </tr>
         </thead>
      </table>
</div>

jquery/js: jQuery/JS:

function loadData(cartIndex, event) {        
    alert($('.cart').index());
    var $cart = $('.cart:eq(' + cartIndex + ')');   
    alert($cart.index()); //returns -1
    $cart.find('.cartcontent').datagrid('loadData', data[cartIndex]);    

}

why is it returning -1 index here and what does this -1 mean?为什么它在这里返回-1索引,这个-1是什么意思?

-1 means the object cannot be found in the array. -1表示在数组中找不到对象。

$cart.index() returns -1 , hence whatever is in $cart can not be found. $cart.index()返回-1 ,因此无法找到$cart任何内容。 One line above, you assign $('.cart:eq(' + cartIndex + ')') to $cart .上面一行,您将$('.cart:eq(' + cartIndex + ')')分配给$cart Hence the selector .cart:eq(' + cartIndex + ')' matches no element in your html.因此选择器.cart:eq(' + cartIndex + ')'匹配你的 html 中的任何元素。

It means the jQuery object $cart does not contain any element这意味着 jQuery 对象$cart不包含任何元素

.index() 。指数()

If the element is not found, .index() will return -1.如果未找到该元素,.index() 将返回 -1。

Thanks everyone for helping out with all the ideas that I couldn't have thought of.感谢大家帮助我提出所有我无法想到的想法。

One of the main reasons the code didn't work as expected was due to the fact that jquery/js script function was called before DOM was loaded.代码未按预期工作的主要原因之一是在加载 DOM 之前调用了 jquery/js 脚本函数。 That's what @Rory McCrossan first commented.这就是@Rory McCrossan 首先评论的内容。 I went through searching and landed on this article: Running Your Code at the Right Time .我通过搜索找到了这篇文章:在正确的时间运行你的代码

@AmmarCSE from my earliest post actually asked me "so you mean you have a click handler attached to .cart and inside you want to load the table?"我最早的帖子中的@AmmarCSE 实际上问我“所以你的意思是你有一个点击处理程序附加到 .cart 并且你想在里面加载表格?”

I had no idea what that was in the context of jquery/javascript and this whole web thing.我不知道在 jquery/javascript 和整个网络事物的上下文中是什么。 However that helped me a lot to understand perhaps I need an eventlistener and to think further why I must use a button click event and how pageload/domload doesn't suit this scenario.不过这对我帮助很大或许明白我需要一个eventlistener ,并想进一步为什么我必须用一个按钮单击事件以及如何页面加载/ domload不适合这种情况。

@Arun P Johny stated many ways to find out if the elements are existing while running the code. @Arun P Johny 陈述了多种方法来确定在运行代码时元素是否存在。

I put the js code execution here:我把js代码执行放在这里:

</body>

    <script>
        document.addEventListener("DOMContentLoaded", theDomHasLoaded, false);
        alert("haha DOM content is loaded");
        function theDomHasLoaded(e) {
            addAll(0, e);
        }
    </script>     
    <script type="text/javascript" src="cart_Test.js"> </script>

</html>

Coming from C# background with fairly comfortable VS IDE with great debugging facilities, I realized debugging jQuery, js codes are rather tedious.来自 C# 背景,具有相当舒适的 VS IDE 和出色的调试工具,我意识到调试 jQuery、js 代码相当乏味。

Since this was a test to place the migration skeleton from jsfiddle to asp.net, I tried out with pageload, domload events.由于这是将迁移框架从 jsfiddle 放置到 asp.net 的测试,因此我尝试了 pageload、domload 事件。 However in actual implementation I want to load these cart html tables on a button click event.但是在实际实现中,我想在按钮单击事件上加载这些购物车 html 表。 By then entire DOM is anyway loaded.到那时,整个 DOM 都已加载。 I learnt alot here today, thanks guys.今天在这里学到了很多,谢谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM