2017-06-03 23 views
4

CSS3によって設定されたDOM要素の::beforeコンテンツを取得できるかどうかを知りたいと思います。DOM要素の:: beforeコンテンツをJavaScriptで取得する方法は?

私はいくつかの方法を試しましたが、私はまだそれを作ることができないので、私には非常に混乱しています!

// https://rollbar.com/docs/ 

const links = document.querySelectorAll(`ul.image-list a`); 

links[0]; 
// <a href="/docs/notifier/rollbar-gem/" class="ruby">::before Ruby</a> 

links[0]; 
// 

links[0].textContent; 
//"Ruby" 

links[0].innerText; 
// "Ruby" 

links[0].innerHTML; 
// "Ruby" 

// ??? links[0]::before; 

これが事実である:

![enter image description here

+0

'getComputedStyle(document.querySelector( "ジャバスクリプト。")、 ":前")。getPropertyValue( "背景画像") ' –

+0

ええ、それは動作します! – xgqfrms

+2

この質問にはどのように投票権がありますか? – Booligoosh

答えて

4

パス":before"window.getComputedStyle()に2番目のパラメータとして:

console.log(getComputedStyle(document.querySelector('p'), ':before').getPropertyValue('content'));
p::before, 
 
p::after { 
 
    content: ' Test '; 
 
}
<p>Lorem Ipsum</p>

+0

ありがとうございました!あなたは素晴らしい仕事をしました! – xgqfrms

1

getComputedStyle()& getPropertyValue()

image

getComputedStyle(document.querySelector('span.search-box'), '::after').getPropertyValue('content');

image

+0

タダ、いい仕事をした! – xgqfrms

関連する問題