Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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 将id为的所有元素放入数组中_Javascript_Jquery - Fatal编程技术网

Javascript 将id为的所有元素放入数组中

Javascript 将id为的所有元素放入数组中,javascript,jquery,Javascript,Jquery,我试图返回一个包含所有元素的数组,该数组的id为id,以某个值开头,位于div中,并将其分配给一个var,但我得到的未定义不是一个函数异常。代码如下: var stopPoints = $("#stop-point-content").querySelectorAll("[id^='stop-point-input-']"); 到达这行代码时,HTML元素确实存在(或至少存在一个) 编辑: 在我的例子中,id类似于stop-point-input-1,stop-point-input-2,等等

我试图返回一个包含所有元素的数组,该数组的id为
id
,以某个值开头,位于
div
中,并将其分配给一个var,但我得到的
未定义不是一个函数
异常。代码如下:

var stopPoints = $("#stop-point-content").querySelectorAll("[id^='stop-point-input-']");
到达这行代码时,HTML元素确实存在(或至少存在一个)

编辑:


在我的例子中,id类似于
stop-point-input-1
stop-point-input-2
,等等。这就是为什么我通过
id^='stop-point-input-'
-从任何值开始搜索。。。请不要在这里关注
css

jQuery包装的对象没有
querySelectorAll
,但是
find
会起到以下作用:

var stopPoints = $("#stop-point-content").find("[id^='stop-point-input-']");
您也可以使用一个选择器完成相同的任务:

var stopPoints = $("#stop-point-content [id^='stop-point-input-']");
但这是一个好迹象,你应该使用一个类来代替:

var stopPoints = $("#stop-point-content .stop-point-input");

Id是用来标识一个特定的元素(其中之一),如果你想针对一组元素,那么我建议使用一个类。我知道Id和类的用途。。。在我的例子中,id类似于
stop-point-input-1
stop-point-input-2
,等等。这就是我通过
id^
搜索的原因-从任何值开始…啊,是的,我现在看到了编辑。