1
0

More than 3 years have passed since last update.

CSSの属性セレクタと否定擬似クラスを同時に使用するサンプル

Last updated at Posted at 2020-09-02

属性セレクタは直感的でよく使うのですが、否定の構文がないので困っていました。調べると否定擬似クラスがあることが分かりました。属性セレクタと組み合わせて使う方法が難しかったのでメモしておきます。

HTML

link要素が2つあります。media属性が存在しないlinkだけ取得する方法の紹介です。

<head>
  <link href="a.css" rel="stylesheet" media="only screen and (max-width: 320px)" >
  <link href="b.css" rel="stylesheet">
</head>

DevTools コンソール

Chromeの開発ツールのコンソールで指定する例


$('head > link[rel="stylesheet"][href]:not([media])').attr('href')

結果


"b.css"

puppeteer

puppeteerで指定する例。セレクタの指定内容はDevToolsと同じです。

const href = await page.$eval('head > link[rel="stylesheet"][href]:not([media])', el => el.href);
console.log(href);

結果

b.css

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0