Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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
Javascript Jquery将li元素添加到数组_Javascript_Jquery_Arrays - Fatal编程技术网

Javascript Jquery将li元素添加到数组

Javascript Jquery将li元素添加到数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有以下html: <ul id="note-item-lists" class="list-group list-group-sp"> <li id="1" class="list-group-item"> <div id="note-4" class="view"> <button class="destroy close hover-action">×</button>

我有以下html:

        <ul id="note-item-lists" class="list-group list-group-sp">
    <li id="1" class="list-group-item">
        <div id="note-4" class="view">
            <button class="destroy close hover-action">×</button>
            <div class="note-name">
                <strong id="new_note_title"> New note </strong>
            </div>
        </div>
    </li>
    <li id="2" class="list-group-item">
        <div id="note-4" class="view">
            <button class="destroy close hover-action">×</button>
            <div class="note-name">
                <strong id="new_note_title"> Old note </strong>
            </div>
        </div>
    </li>
  </ul>

然而,当我调试时,我得到一个空的
数组
有什么想法吗?

你就快到了
id
是DOM节点的属性,而不是jQuery对象的属性,因此应该使用
this.id
而不是
$(this.id

var notes = [];
$('.list-group-item').each(function() {
    notes[this.id] = $(this);
});
请记住,您的第一个
li
元素的
id
1
,但Javascript数组的索引为零,因此
notes
(即
notes[0]
)的第一个元素将是
未定义的
。我建议按它们出现的顺序将它们推到阵列上:

var notes = [];
$('.list-group-item').each(function() {
    notes.push($(this));
});

你快到了
id
是DOM节点的属性,而不是jQuery对象的属性,因此应该使用
this.id
而不是
$(this.id

var notes = [];
$('.list-group-item').each(function() {
    notes[this.id] = $(this);
});
请记住,您的第一个
li
元素的
id
1
,但Javascript数组的索引为零,因此
notes
(即
notes[0]
)的第一个元素将是
未定义的
。我建议按它们出现的顺序将它们推到阵列上:

var notes = [];
$('.list-group-item').each(function() {
    notes.push($(this));
});

如果您正在chrome控制台中进行检查,则应将其声明为对象,因为您正在将其用作对象。Chrome对控制台中的数组很奇怪(实际上,它们做得很好,因为javascript中的数组基本上都是对象,并且本身没有关联数组):

但是:


如果您正在chrome控制台中进行检查,则应将其声明为对象,因为您正在将其用作对象。Chrome对控制台中的数组很奇怪(实际上,它们做得很好,因为javascript中的数组基本上都是对象,并且本身没有关联数组):

但是:


也许您只需要:
var$lis=$('.list-group-item')
。这比保存DOM元素的数组要好,它是一个jQuery集合。你想用这些身份证做什么?为什么它们对数组很重要?@elclanrs上面是一个php生成的长列表示例:)实际应用程序将有数百个项目,其中id与数据库引用的id匹配为什么不能将DOM保存在jQuery集合中。当需要获取某些元素并检索它们的ID时,过滤集合,映射ID,然后
.toArray
。这是我的建议,请尽可能长时间地保留jQuery集合。@elclars谢谢,我会给它读一读也许你只是想:
var$lis=$('.list-group-item')
。这比保存DOM元素的数组要好,它是一个jQuery集合。你想用这些身份证做什么?为什么它们对数组很重要?@elclanrs上面是一个php生成的长列表示例:)实际应用程序将有数百个项目,其中id与数据库引用的id匹配为什么不能将DOM保存在jQuery集合中。当需要获取某些元素并检索它们的ID时,过滤集合,映射ID,然后
.toArray
。这是我的建议,请尽可能长时间地保留jQuery集合。@elclans谢谢,我会给它一个read。notes不是数组,它应该是数组object@Peter谢谢你的帮助!以后搜索时需要该id。但是我认为javascript没有任何可以与hashmap相比的东西?是吗?是的,在Javascript中它们被称为对象。您可以使用
var notes=[]
而不是
var notes={}
将其用作hashmap。(请注意,
[]
{}
new Array()
new Object()
更快、更短)@ArunPJohny-
notes
可以是数组,因为ID是数字的。但是从你之后的评论来看,OP似乎确实想要一个对象……而且notes不是数组,它应该是一个数组object@Peter谢谢你的帮助!以后搜索时需要该id。但是我认为javascript没有任何可以与hashmap相比的东西?是吗?是的,在Javascript中它们被称为对象。您可以使用
var notes=[]
而不是
var notes={}
将其用作hashmap。(请注意,
[]
{}
new Array()
new Object()
更快、更短)@ArunPJohny-
notes
可以是数组,因为ID是数字的。但从你之后的评论来看,OP似乎确实想要一个对象。。。
x = {}
//Object {}
x['a'] = 1
//1
x
//Object {a: 1}