简体   繁体   English

我怎样才能捕获:IE8中的内容之后?

[英]How can I capture :after content in IE8?

I am applying an :after pseudo element to the body displaying the name of my media query breakpoint like so: 我申请一个:after伪元素主体显示我的媒体查询断点的名字,像这样:

body::after {
  content: 'medium';
  display: none;
}

The reason for doing this can be found here: http://adactio.com/journal/5429/ 这样做的原因可以在这里找到: http//adactio.com/journal/5429/

I want to get the content value of :after using javascript in IE8. 我想得到的内容值:after在IE8中使用javascript :after

This is how i am doing it for other browsers: 这是我为其他浏览器做的方式:

var breakpoint = window.getComputedStyle(document.body, ':after').getPropertyValue('content');

But IE8 does not support getComputedStyle() , i know it supports currentStyle instead, but after a bit of trying i was unable to use it correctly. 但IE8不支持getComputedStyle() ,我知道它支持currentStyle ,但经过一些尝试后我无法正确使用它。

This is the kind of thing i was trying with no success: 这是我尝试没有成功的事情:

var breakpoint = document.body.currentStyle.getPropertyValue('content');

Anybody know how to do this? 有人知道怎么做吗?

Edit: After BoltClock's note i have now changed my css to this (one semi colon): 编辑:在BoltClock的笔记之后我现在已经将我的css改为此(一个半冒号):

body:after {
  content: 'medium';
  display: none;
}

Before using two the content was not even appearing in IE8, so it would have had nothing to return. 在使用两个之前,内容甚至没有出现在IE8中,所以它没有任何回报。 Unfortunately i still can't get IE8 to return the content. 不幸的是我仍然无法让IE8返回内容。

I am trying this: 我在尝试这个:

if (style = document.body.currentStyle) {
  for (var prop in style) {
    if (prop === 'content') {
      alert(prop);
    }
  }
}

I get nothing, but if i change 'content' to some other property like 'backgroundColor' it will alert something. 我什么也没得到,但如果我将'content'改为'backgroundColor'之类'backgroundColor'其他属性,它会发出警告。 So i'm thinking that even though msdn lists content as one of the available properties of currentStyle http://msdn.microsoft.com/en-us/library/ie/ms535231%28v=vs.85%29.aspx it does not actually return it, unless i'm doing something else wrong. 所以我想虽然msdn将内容列为currentStyle的可用属性之一http://msdn.microsoft.com/en-us/library/ie/ms535231%28v=vs.85%29.aspx但它确实如此实际上并没有归还它,除非我做错了什么。

After a lot of trying i've come to the conclusion that it is not possible. 经过大量的尝试,我得出的结论是,这是不可能的。 But thankfully i have found a js script which is achieving exactly what i was after. 但幸运的是,我找到了一个js脚本,它正在实现我所追求的目标。

https://github.com/JoshBarr/js-media-queries https://github.com/JoshBarr/js-media-queries

As well as using Jeremy Keith's technique of putting the breakpoint label in the body:after they have put the breakpoint label in font-family on the html element. 除了使用Jeremy Keith将断点标签放在正文中的技术之后:他们将断点标签放在html元素的font-family中。 This enables support for ie8 and some cases where android was not able to return the :after content either. 这支持ie8和某些情况下android无法返回内容之后的内容。

From IE7 to IE11, you can "create your own CSS Properties", and just access them by using the currentStyle object 从IE7到IE11,您可以“创建自己的CSS属性”,并使用currentStyle对象访问它们

Then a solution is to create a "fake content property" 然后一个解决方案是创建一个“假内容属性”

body:after
{
    content: 'medium';
    -ms-content: 'medium';
}

I called it "-ms-content" for convenience, now your JavaScript code for IE should be: 为方便起见,我把它称为“-ms-content”,现在你的IE的JavaScript代码应该是:

document.body.currentStyle["-ms-content"] // RETURNS => 'medium'

You must access "your own property" by using the brackets: currentStyle[{String}] otherwise you will get an empty string 您必须使用括号访问“您自己的属性”:currentStyle [{String}]否则您将获得一个空字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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