简体   繁体   English

使用document.querySelector和复杂的CSS选择器

[英]using document.querySelector with complex CSS selectors

In JavaScript I want to use document.querySelector to "grab" the last div ( <div class="widget-footer"> ) in below HTML. 在JavaScript中,我想使用document.querySelector来“抓取” HTML下面的最后一个div( <div class="widget-footer"> )。 However after many tries, I still can't figure out the correct CSS selector syntax to use. 但是,经过多次尝试,我仍然无法弄清楚要使用的正确CSS选择器语法。

The following code does not work: 以下代码不起作用:

document.querySelector (".skin-grid-widgets.ui-sortable.gridWidgetTemplatePositie.AgendaStandaard.disablesorting.hoogte-1-knoppen-0.breedte-1.widget-footer")

Here is the HTML I am working with 这是我正在使用的HTML

<div class="skin-grid enkeleKolom" id="Infobalk">
    <div class="skin-grid-widgets ui-sortable">
        <div class="gridWidgetTemplatePositie AgendaStandaard disablesorting hoogte-1-knoppen-0 breedte-1">
            <div class="widget-header">
                here comes the header text
             </div>
            <div class="widget-body">
                some body text
            </div>   
            <div class="widget-footer">
                here comes the footer text
            </div>
        </div>
    </div>
</div>

I've surfed everywhere to find example of complex CSS selectors used with querySelector, but to no avail. 我到处冲浪以查找与querySelector一起使用的复杂CSS选择器的示例,但无济于事。 Any help would be really appreciated. 任何帮助将非常感激。

Your issue is you need a space in between each child element you are trying to select. 您的问题是,您要选择的每个子元素之间都需要有一个空格。 If you do not have spaces in between your class selectors, by CSS specification, it will look for both classes on the same element. 如果在类选择器之间没有空格,则根据CSS规范,它将在同一元素上查找两个类。

Change your selector to look like the following: 将选择器更改为如下所示:

 var footer = document.querySelector(".skin-grid-widgets.ui-sortable .gridWidgetTemplatePositie.AgendaStandaard.disablesorting.hoogte-1-knoppen-0.breedte-1 .widget-footer"); footer.classList.add("highlight"); 
 .highlight { background-color: yellow; } 
 <div class="skin-grid enkeleKolom" id="Infobalk"> <div class="skin-grid-widgets ui-sortable"> <div class="gridWidgetTemplatePositie AgendaStandaard disablesorting hoogte-1-knoppen-0 breedte-1"> <div class="widget-header"> here comes the header text </div> <div class="widget-body"> some body text </div> <div class="widget-footer"> here comes the footer text </div> </div> </div> </div> 

try this: 尝试这个:

<script>
   document.querySelector (".skin-grid-widgets .gridWidgetTemplatePositie .widget-footer");
</script>

You don't need to add adjacent classes like "skin-grid-widgets ui-sortable" in querySelector, if you do so then query selector assumes that "skin-grid-widgets" is parent of "ui-sortable". 您不需要在querySelector中添加类似“ skin-grid-widgets ui-sortable”的相邻类,如果这样做,则查询选择器会假定“ skin-grid-widgets”是“ ui-sortable”的父类。 Use just one of the classes at one DOM level. 在一个DOM级别仅使用这些类之一。

The selector ain't complex, your thoughts are. 选择器并不复杂,您的想法很复杂。

Listen to yourself, to the description you provide of what you want to select: 倾听自己,听取您对要选择的内容的描述:

"grab" the last div in below HTML “抓取” HTML下面的最后一个div

Not grab the node with the class widget-footer inside of a node that has all these classes: gridWidgetTemplatePositie AgendaStandaard disablesorting hoogte-1-knoppen-0 breedte-1 , inside a node ... 不要在具有所有这些类的节点内部使用类widget-footer捕获该节点: gridWidgetTemplatePositie AgendaStandaard disablesorting hoogte-1-knoppen-0 breedte-1在节点内部...

//a utility, because DRY.
//and because it's nicer to work with Arrays than with NodeLists or HTMLCollections.
function $$(selector, ctx=document){
    return Array.from(ctx.querySelectorAll(selector));
}

//and the last div in this document:
var target = $$('div').pop();

or 要么

"grab" <div class="widget-footer"> in below HTML HTML下面的“抓取” <div class="widget-footer">

var target = document.querySelector("div.widget-footer");

or the combination: grab the last div.widget-footer in the HTML 或组合:获取HTML中的最后一个div.widget-footer

var target = $$('div.widget-footer').pop();

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

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