Javascript 按ID抓取div并应用内联样式
我有一个带有Javascript 按ID抓取div并应用内联样式,javascript,Javascript,我有一个带有id=“playerViewer”的div 在某些情况下,我希望更改此div的样式,我尝试这样做: if (include(urlarr, listen)) { //$('#playerViewer').css('position', 'fixed').css('top', '5px').css('right', '100px'); I Don't want to use Jquery document.getElementById("player
id=“playerViewer”
的div
在某些情况下,我希望更改此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>
当通过css隐藏div时,是否可以访问div(例如
显示:无
)
您可以在页面加载后尝试执行代码:
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尚未创建/无法访问。显示您的完整HTML代码。请提供更多信息和HTML,可能是一个提琴手您是否使用Asp.net与母版页?我看到的唯一原因是当脚本执行时,div可能没有加载到DOM您可以使用文档体顶部的脚本。