html - 需要 XPATH 表达式的等效 CSS 选择器

标签 html css xpath css-selectors

我有一个 html 代码如下:

 <div id="select_a_boundary" class="dataset_select2">Homes name</div>

我为此写了一个 xpath 表达式:

 //div[@id = 'select_a_boundary' and @class = 'dataset_select2']

相同的 CSS 选择器是什么?

最佳答案

首先,如果您正在使用 id,则不需要使用类,其次,如果您愿意,您可以选择具有 id select_a_boundary 的元素使用

#select_a_boundary {
   /* Styles goes here */
}

Demo

Note: Am not selecting the element which has that id and that class as here, id is sufficient as it has to be unique, if you are using the id for multiple elements than it's invalid


根据您的意见

div[id=select_a_boundary][class=dataset_select2] {
    color: red;
}

Demo X-Path Equivalent

或者更简单的一个(来源:Jack)

#select_a_boundary.dataset_select2 {
    color: red;
}

Note: Still I would recommend you to use #select_a_boundary is more than enough

关于html - 需要 XPATH 表达式的等效 CSS 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17828871/

相关文章:

javascript - 如何获取转到另一个页面上的特定 div 的链接

javascript - 在由 javascript 加载的图像上添加文本

CSS三 Angular 形如何去除右边的空白

java - TagSoup 和 XPath

jquery - 当用户滚动到元素时,会触发动画

c# - ASP :FileUpload edit "No file selected" message

javascript - CSS 不适用于 rzslider

php - 如何使用 PHP/XPath/DOM 查询 namespace

java - XPath.evaluate 性能在多次调用中减慢(荒谬地)

html - ul 的子元素的组合宽度等于它们的总和