简体   繁体   English

jQuery选择器找到DOM元素,但本机javascript选择器返回未定义

[英]Jquery selector finds the DOM element but native javascript selector returns undefined

I was using the native Javascript selector on a div element like following 我在div元素上使用本机Javascript选择器,如下所示

var slider_image=document.querySelectorAll('.slider');
    slider_image.style.opacity=0;

This returns me uncaught typo error. 这会返回我未捕获的拼写错误。 Cannot set the opacity of undefined. 无法设置未定义的不透明度。 However, when I find the div element using the selector in the console, I get the DOM. 但是,当我在控制台中使用选择器找到div元素时,便获得了DOM。 But again I can't set the opacity values as it shows the same error. 但是我不能设置不透明度值,因为它显示了相同的错误。

However, the jquery selector perfectly does the job. 但是,jQuery选择器可以完美地完成这项工作。 What is the problem here? 这里有什么问题?

Could anyone suggest? 有人可以建议吗?

Thanks 谢谢

Because querySelectorAll returns a list and list doens't have the properties you are looking for. 因为querySelectorAll返回一个列表,并且列表没有您要查找的属性。

Where as Jquery maintains the collections of selected Objects and do things internally and apply the changes you asked for. 其中,Jquery维护选定对象的集合,并在内部进行操作并应用您要求的更改。

However you can do the same with Native script aswell. 但是,您也可以使用本机脚本执行相同的操作。 All you need to do is iterate over the returned list and apply the styles on each object instead of applying it to the list directly. 您需要做的就是遍历返回的列表,然后将样式应用于每个对象,而不是直接将其应用于列表。

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

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