简体   繁体   English

如何选择/使用元素的子元素。 Javascript

[英]How do I select/work with the child of an element. Javascript

I have a header in my html that has two different classes.我的 html 中有一个 header,它有两个不同的类。 Depending on when I scroll down it changes.根据我向下滚动的时间,它会发生变化。 IN CSS u select descendent with space. IN CSS u select 带空格的后代。 In my case .navbar.menu-toggler and .nav_sticky.menu-toggler .在我的例子.navbar.menu-toggler.nav_sticky.menu-toggler I succeded to select .navbar via var h = document.getElementsByClassName(id3)[0];我通过var h = document.getElementsByClassName(id3)[0];成功到 select .navbar ; (id3 being "navbar") but i want to change the style of .menu-toggler And i can't write directly .menu-toggler because i need it when it's withing the class of .navbar .I over complicated the explanation but I hope somebody can help (id3 是“导航栏”)但我想更改.menu-toggler的样式我不能直接写.menu-toggler因为当它与 .navbar 的.navbar时我需要它。我过于复杂的解释但我希望有人能帮忙

oH and how can i add a class and remove a class to an element哦,我如何向元素添加 class 和删除 class

I have a header in my html that has two different classes.我的 html 中有一个 header,它有两个不同的类。 Depending on when I scroll down it changes.根据我向下滚动的时间,它会发生变化。 IN CSS u select descendent with space.在 CSS u select 后代与空格。 In my case .navbar.menu-toggler and .nav_sticky.menu-toggler .在我的情况下.navbar.menu-toggler.nav_sticky.menu-toggler I succeded to select .navbar via var h = document.getElementsByClassName(id3)[0];我通过var h = document.getElementsByClassName(id3)[0];成功到 select .navbar ; (id3 being "navbar") but i want to change the style of .menu-toggler And i can't write directly .menu-toggler because i need it when it's withing the class of .navbar .I over complicated the explanation but I hope somebody can help (id3是“navbar”)但我想改变.menu-toggler的风格而且我不能直接写.menu-toggler ,因为当它与.navbar的.navbar使用时我需要它。我过度复杂的解释但我希望有人能帮忙

oH and how can i add a class and remove a class to an element哦,我怎样才能添加一个 class 并删除一个 class 到一个元素

I guess you can use document.querySelector to access the toggler.我想您可以使用 document.querySelector 来访问切换器。

var menuToggler = document.querySelector(".navbar.menu-toggler");

You'll get the item which has.navbar and.menu-toggler inside it's classList.您将在其 classList 中获得具有 .navbar 和 .menu-toggler 的项目。

I don't thing accessing children by indexes is a good practice.我不认为通过索引访问孩子是一个好习惯。 If your HTML document gets updated, your index might point to another DOM element.如果您的 HTML 文档得到更新,您的索引可能指向另一个 DOM 元素。

Check this answer for more information https://stackoverflow.com/a/59406548/9517443查看此答案以获取更多信息https://stackoverflow.com/a/59406548/9517443

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

相关问题 如何更改在父元素中具有ng-click的子元素中的变量。 - How do I change a variable in a child element that has ng-click in the parent element. 尝试选择特定元素。 如何将所有其他未选择的元素更改为 false? - Trying to select a specific element. How do I change all other elements that aren't selected to false? 无法在嵌入式 Twitch 元素中禁用 javascript 中的自动播放。 我该如何解决? - Can't disable autoplay in javascript in an embedded Twitch element. How do I fix this? 如何选择元素的第n个子元素? - How do I select the nth child of an element? 如何选择DuckDuckGo元素。 AJAX - How to Select DuckDuckGo Element. AJAX 如何获取元素的类型。 JavaScript / jQuery - How to get the type of an element. JavaScript / jQuery 如何选择父元素的第一个子元素? - How do I select the first child of a parent element? 如何使用jQuery选择具有特定子元素的元素? - How do I select an element having a specific child using jQuery? 子元素的宽度可以覆盖父元素设置的最大宽度。 我怎样才能防止这种情况发生? - Width of child element is able to override max-width set by parent element. How can I prevent this from happening? 如何触发元素上的浏览器上下文菜单。 (就像用户右击一样) - How do I trigger the browser context menu on an element. (As if the user right-clicked)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM