[英]Javascript to put centered text at the bottom of the page
我正在嘗試使用javascript在屏幕底部插入一些居中對齊的文本。
到目前為止,我有
var toInsert = document.createElement("div");
toInsert.innerHTML = "text to insert";
toInsert.style.position = "absolute";
toInsert.style.bottom = "0px";
這會將其放置在屏幕底部,但我想要使用它時
toInsert.style.textAlign="center";
中心對齊方式似乎被上面的“ absolute”屬性所覆蓋,並且文本左對齊。
如何將文本放在屏幕底部並居中? 謝謝。
PS。 JavaScript,而不是jquery,用於iOS設備上的UIWebView。
確保您的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);
IMO會更容易使用類標識符在CSS中正確定位元素,然后使用javascript添加類。
由於您使用的是絕對定位,因此您要查找的CSS可能是:
left:50%;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.