如何使用 CSS 选择器与 :not 运算符部分匹配
How to use CSS selector partial match with :not operator
我想要获取 Nokogiri
不包含特定字符串的元素。
doc = Nokogiri::HTML('<div id="foo"></div><div id="bar"></div>')
doc.css('*[id*="foo"]')
# => <div id="foo"/>
doc.css('*:not[id*="foo"]')
# => []
我以为 *:not[id*="foo"]
returns 元素有一个 id bar
,但它没有。
如何同时使用部分匹配和 :not
运算符?
这是 documentation and here's a corresponding answer: "CSS use :not ID with CLASS" :
doc.css('div:not(#foo)')
将为您提供 id
不是 foo
的所有 div。
doc.css('*:not(#foo)')
将为您提供 id
不是 foo
的所有节点。
doc.css('div:not([id*="foo"])')
将为您提供所有 id
不包含 foo
.
的 div
require 'nokogiri'
doc = Nokogiri::HTML('<div id="foo"></div><div id="bar"></div><div id="foofoo"></div>')
puts doc.css('div:not([id*="foo"])')
#=> <div id="bar"></div>
您在否定条件中缺少一些括号。使用 :not(whatever)
doc.css('*[id*="foo"]') # All elements with foo contained in the id
doc.css('*:not([id*="foo"])') # All elements without foo contained in the id
我想要获取 Nokogiri
不包含特定字符串的元素。
doc = Nokogiri::HTML('<div id="foo"></div><div id="bar"></div>')
doc.css('*[id*="foo"]')
# => <div id="foo"/>
doc.css('*:not[id*="foo"]')
# => []
我以为 *:not[id*="foo"]
returns 元素有一个 id bar
,但它没有。
如何同时使用部分匹配和 :not
运算符?
这是 documentation and here's a corresponding answer: "CSS use :not ID with CLASS" :
doc.css('div:not(#foo)')
将为您提供 id
不是 foo
的所有 div。
doc.css('*:not(#foo)')
将为您提供 id
不是 foo
的所有节点。
doc.css('div:not([id*="foo"])')
将为您提供所有 id
不包含 foo
.
require 'nokogiri'
doc = Nokogiri::HTML('<div id="foo"></div><div id="bar"></div><div id="foofoo"></div>')
puts doc.css('div:not([id*="foo"])')
#=> <div id="bar"></div>
您在否定条件中缺少一些括号。使用 :not(whatever)
doc.css('*[id*="foo"]') # All elements with foo contained in the id
doc.css('*:not([id*="foo"])') # All elements without foo contained in the id