使用XPath选择具有最高z-index值的项目

时间:2018-11-05 19:01:57

标签: html xpath

需要使用具有最高z-index值的选择器

<div style="position: fixed;width: 400px;height: 278px;overflow: hidden;box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 6px;bottom: 0px;transition: transform 2s ease 0s;right: 120px;z-index: 2;" class="Cl"><iframe id="t1dxvk58p7cu" name="t1dxvk58p7cu" class="Xyqxtc" allow="camera" style="height: 100%; width: 100%; background: transparent; overflow: hidden; border: none;"></iframe></div>
<div style="position: fixed;width: 400px;height: 278px;overflow: hidden;box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 6px;bottom: 0px;transition: transform 2s ease 0s;right: 120px;z-index: 3;" class="Cl"><iframe id="t1dxvk58p7cu" name="t1dxvk58p7cu" class="Xyqxtc" allow="camera" style="height: 100%; width: 100%; background: transparent; overflow: hidden; border: none;"></iframe></div>
<div style="position: fixed;width: 400px;height: 278px;overflow: hidden;box-shadow: rgba(0, 0, 0, 0.2) 0px 2px 6px;bottom: 0px;transition: transform 2s ease 0s;right: 120px;z-index: 4;" class="Cl"><iframe id="t1dxvk58p7cu" name="t1dxvk58p7cu" class="Xyqxtc" allow="camera" style="height: 100%; width: 100%; background: transparent; overflow: hidden; border: none;"></iframe></div>

我在下面尝试过但失败了

//div[@class='Cl'][substring-after(@style,'z-index:') > substring-after(../div[@class='Cl']/@style,'z-index:')]

1 个答案:

答案 0 :(得分:0)

Here is an XPath 2.0 solution using regex replace and max

//div[@class='Cl' and replace(@style,'^.*?z-index: (\d+).*?$','$1') = max(../div[@class='Cl']/replace(@style,'^.*?z-index: (\d+).*?$','$1'))]

the main idea is

  • extract the z-index from the style-attribute using replace
  • and compare it to the maximum z-index of similar elements: div[id = max(../div/id)]
    (id equals the extracted z-index)

Live Demo

这是遵循模式div/[not(..div/id > id)]的另一个:

//div[@class='Cl' and not(../div[@class='Cl']/replace(@style,'^.*?z-index: (\d+).*?$','$1') > replace(@style,'^.*?z-index: (\d+).*?$','$1'))]

参考文献: