function cc()
{
var cf=window.screen.width;
alert(cf)
if(cf<=1920)
{
cf=-(1920-cf)/2
}
else if(cf>1920)
{
cf=(cf-1920)/2
}
document.getElementById("m1").style.marginLeft=cf;
alert(cf)
}
script>
上面可以给M1进行左移赋值:document.getElementById("m1").style.marginLeft=cf;
但在第一行加这行后:
M1就赋值失败,请问一般HTML文件都有,如何处理?
回复讨论(解决方案)
document.getElementById("m1").style.marginLeft=cf +"px";
document.getElementById("m1").style.marginLeft=cf +"px";
可以了,多谢!
改为
为何用document.getElementsByTagName("m1").style.marginLeft=cf+"px";赋值不了?
6
document.getElementById("m1").style.marginLeft=cf +"px";
可以了,多谢!
改为
为何用document.getElementsByTagName("m1").style.marginLeft=cf+"px";赋值不了?
getElementsByTagName()是用标签名来选择
用class是
document.getElementsByClassName("m1")[0].style.marginLeft=cf+"px";
或者
document.querySelector(".m1").style.marginLeft=cf+"px";
受教了,多谢!