2017-06-03 132 views
4

我想知道是否有可能獲取由CSS3設置的DOM元素的::before內容。如何在JavaScript之前獲取DOM元素的::之前的內容?

我已經嘗試了一些方法,但我仍然無法做到,所以對我來說非常困惑!

// 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( 「JavaScript的。」), 「:前」)。getPropertyValue( 「background-image」)' –

+0

是的,它的工作原理! – xgqfrms

+2

這個問題沒有更多的投票? – Booligoosh

回答

4

通行證":before"作爲第二個參數,以window.getComputedStyle()

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

相關問題