0

我有一个带有id="playerViewer".

在某些情况下,我想更改此 div 的样式,我尝试这样做:

  if (include(urlarr, listen)) {
        //$('#playerViewer').css('position', 'fixed').css('top', '5px').css('right', '100px'); I Don't want to use Jquery
        document.getElementById("playerViewer").style.position="fixed";
        document.getElementById("playerViewer").style.top="5px";
        document.getElementById("playerViewer").style.right="100px";
        alert("No alert showing up, even though the function is true");
};

在我的控制台中,我得到:

 Uncaught TypeError: Cannot read property 'style' of null

所以我想我用getElementById错了——有人能指出我正确的方向吗?

HTML:

<div id="playerViewer">
    <div id="defaultVideocontainer">
        <div id="defaultVideopart">
        <video id='dago_video' class='jwplayer dago-video'  width='480' height='360' controls ></video>
        </div>
    </div>
</div>
4

1 回答 1

1

无论您是否可以访问一个 div,无论它是否通过 css 隐藏(例如display:none),都没有任何事情要做。

您可以尝试在页面加载后执行代码:

window.onload = function() {
  if (include(urlarr, listen)) {
        document.getElementById("playerViewer").style.position="fixed";
        document.getElementById("playerViewer").style.top="5px";
        document.getElementById("playerViewer").style.right="100px";
  };
}

如果您之前执行它,则该 div 尚未创建/无法访问。

于 2013-08-14T13:03:37.387 回答