“无法读取属性'content'of null”:Psuedo Elements [英] "Cannot read property 'content' of null": Psuedo Elements

查看:151
本文介绍了“无法读取属性'content'of null”:Psuedo Elements的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了这里,以检测在我的Phonegap应用程序的方向更改(加载不同的CSS文件的横向和纵向)。我得到一个无法读取属性的内容的null错误,暗示有一些错误的DOM调用。代码涉及添加这些伪元素到主CSS文件:

I adapted some code from here in order to detect orientation changes in my Phonegap app (to load different CSS files for landscape and portrait). I'm getting a "cannot read property 'content' of null error, implying there's something wrong with the DOM call. The code involves adding these psuedo elements to the primary CSS file:

/*Orientation Detection Hack*/
body:after {
content: "";
position: absolute;
color: transparent;
}

@media all and (orientation: portrait) {
body:after {
content: "p";
    }
}

@media all and (orientation: landscape) {
  body:after {
    content: "l";
    }
}
/*end of orientation detection*/

并且这个事件监听器检查伪元素方向更改:

And this event listener to check the pseudo element on an orientation change:

/*ORIENTATION CHANGES*/
    //get style of the body:after element
var bodyAfterStyle = window.getComputedStyle(document.body, ":after");

window.onorientationchange = function() {


  navigator.notification.alert(
        'ORIENTATION CHANGELOL',  // message
        'Credits',            // title
        'Credits');              // buttonName

  if(bodyAfterStyle.content == 'p') {

/*whatever
    navigator.notification.alert("Now in Portrait");
    var sheet = document.createElement('link');
    sheet.innerHTML = 'href=\"portrait.css\" rel=\"stylesheet\"  type=\"text/css\"';
    document.body.appendChild(sheet);
end whatever*/
} 
    else if(bodyAfterStyle.content == 'l') {
    //do landscape stuff
  }
}
    /*END OF ORIENTATION STUFF*/


推荐答案

看来bodyAfterStyle丢失了,我建议将它的赋值移动到事件函数中。

it seems that bodyAfterStyle is lost, i would propose to move its assignment into the event function.

这篇关于“无法读取属性'content'of null”:Psuedo Elements的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆