简体   繁体   中英

How to get a DOM element's ::before content with JavaScript?

I want to know whether it is possible to get a DOM element's ::before content, which was set by CSS3.

I have tried some ways, but I still can't make it, so it's very confusing to me!

// 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;

This is the case:

![在此处输入图像说明

Pass ":before" as the second parameter to window.getComputedStyle() :

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

getComputedStyle() & getPropertyValue()

图片

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

图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM