我试图使用javascript在屏幕底部插入一些中间对齐的文本。
到目前为止我已经
var toInsert = document.createElement("div");
toInsert.innerHTML = "text to insert";
toInsert.style.position = "absolute";
toInsert.style.bottom = "0px";这就把它放在了屏幕的底部,这是我想要的,但是当我尝试使用这个
toInsert.style.textAlign="center";中心对齐似乎被上面的“绝对”属性覆盖,文本是左对齐的。
我怎样才能把文字放在屏幕的底部,并使其居中呢?谢谢。
ps。javascript而不是jquery,用于UIWebView设备上的iOS。
发布于 2014-11-17 20:38:31
确保您的div是100%宽度。
toInsert.style.width = "100%";
var toInsert = document.createElement("div");
toInsert.innerHTML = "text to insert";
toInsert.style.position = "absolute";
toInsert.style.bottom = "0px";
toInsert.style.textAlign = "center";
toInsert.style.width = "100%";
document.body.appendChild(toInsert);
发布于 2014-11-17 20:29:51
使用类标识符在CSS中正确定位元素将更容易,然后使用javascript添加类。
由于您使用的是绝对定位,所以您要寻找的CSS可能是:
left:50%;发布于 2014-11-17 20:37:01
https://stackoverflow.com/questions/26981201
复制相似问题