这是为什么会生效的原因:
我也尝试过:
这是我的完整代码:
newDiv.style.top = topBar.style.height.split("px")[0]+"px";
->>><div style="top: 30.31px;" class="lineSeparator"></div>
这个也可以正常工作
newDiv.style.top = topBar.style.height.split("px")[0]-2+"px";
->>><div style="top: 28.31px;" class="lineSeparator"></div>
而这也可行
newDiv.style.top = topBar.style.height.split("px")[0]/2+"px";
->>><div style="top: 15.15px;" class="lineSeparator"></div>
但这样是不起作用的:
newDiv.style.top = topBar.style.height.split("px")[0]+2+"px";
->>><div style="top: 30.31px;" class="lineSeparator"></div>
我也尝试过:
newDiv.style.top = (topBar.style.height.split("px")[0]+2)+"px";
这是我的完整代码:
function generateSeparators(n){
for(var i=0;i<n;i++){
var newDiv=document.createElement("div");
newDiv.style.top = topBar.style.height.split("px")[0]+"px";
newDiv.className = "lineSeparator";
sideBar.appendChild(newDiv);
}
}