最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom - Javascript: check for ::after in element - Stack Overflow

programmeradmin5浏览0评论

I have a very simple element:

How can I check if this element contains the ::after using Javascript?

(I have the element using document.getElementsByTagName('h2')[0])

I don't know how the thing works, and I don't need to know, I just need the code to see if it exists...

In some cases the element looks like this:

I need to be able to detect both cases, if possible!

I have a very simple element:

How can I check if this element contains the ::after using Javascript?

(I have the element using document.getElementsByTagName('h2')[0])

I don't know how the thing works, and I don't need to know, I just need the code to see if it exists...

In some cases the element looks like this:

I need to be able to detect both cases, if possible!

Share Improve this question edited Nov 5, 2015 at 21:38 pnuts 59.5k11 gold badges91 silver badges141 bronze badges asked Oct 16, 2015 at 23:02 ManIkWeetManIkWeet 1,3381 gold badge17 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

It would be something like this:

var pre = onload; // assign previous onload if any
onload = function(){ // onload wrapper start
if(pre)pre(); // execute previous onload in the new onload
var doc = document, h2a = doc.getElementsByTagName('h2');
var afterIn = [], afterOut = [];
for(var i=0,l=h2a.length; i<l; i++){
  var h2 = h2a[i];
  if(h2.value.match(/\:\:after/)){
    afterIn.push(h2);
  }
  else{
    afterOut.push(h2);
  }
}
for(var i=0,l=afterIn.length; i<l; i++){
  console.log(afterIn[i].value);
}
for(var i=0,l=afterOut.length; i<l; i++){
  console.log(afterOut[i].value);
}
} // onload wrapper end

Check this link, explains how to use it in plain JavaScript (might only work in newer browsers): http://davidwalsh.name/pseudo-element

Edit: if you're looking to edit what the content of the :after is, reference this SO question: Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery

发布评论

评论列表(0)

  1. 暂无评论