admin管理员组文章数量:1327995
How can I align the entire script to center? I've never actually coded before in javascript and I just want to modify a quick bookmarklet. I've tried stuff like align=center for different ponents of the script but no luck. Is there something like <center>
(in html) for js?
Thanks
EDIT: basically i want this to be centered on a page
function dEmbed(){
var iframe = document.createElement("iframe");
iframe.src = '';
iframe.style.width = w+'px';
iframe.style.height= h+'px';
iframe.style.border= "0";
iframe.style.margin= "0";
iframe.setAttribute("scrolling","no");
iframe.setAttribute("id","NAME");
document.body.insertBefore(iframe, document.body.firstChild);
}
How can I align the entire script to center? I've never actually coded before in javascript and I just want to modify a quick bookmarklet. I've tried stuff like align=center for different ponents of the script but no luck. Is there something like <center>
(in html) for js?
Thanks
EDIT: basically i want this to be centered on a page
function dEmbed(){
var iframe = document.createElement("iframe");
iframe.src = 'http://www.google.';
iframe.style.width = w+'px';
iframe.style.height= h+'px';
iframe.style.border= "0";
iframe.style.margin= "0";
iframe.setAttribute("scrolling","no");
iframe.setAttribute("id","NAME");
document.body.insertBefore(iframe, document.body.firstChild);
}
Share
Improve this question
edited Oct 2, 2011 at 8:25
Cody
asked Oct 2, 2011 at 8:01
CodyCody
8904 gold badges21 silver badges38 bronze badges
2
- Please give us more details - your terminology is all mixed up and it's hard to understand what you are looking to do. (Especially please don't use the word script the way you do.) – Ariel Commented Oct 2, 2011 at 8:02
- Check my edited answer below, I solved your problem. – Mark Kramer Commented Oct 2, 2011 at 21:06
3 Answers
Reset to default 1I was able to achieve this by injecting the iframe into an existing div with text-align: center
.
Here is the example: http://jsfiddle/MarkKramer/PYX5s/4/
You can't centre a script, it doesn't look like anything so there is nothing to centre.
If the script generates some HTML, then you can to centre the generated elements. This should be done using the usual CSS techniques applied to the generated element.
(<center>
and the align
attribute were deprecated in 1998 and shouldn't be used).
Try changing:
iframe.style.margin= "0";
To:
iframe.style.margin= "auto";
If it doesn't work, let me know in a ment.
OK, try this change instead, change:
var iframe = document.createElement("iframe");
to:
var div = document.createElement("div");
div.style.width = w+'px';
div.style.margin= "auto";
var iframe = document.createElement("iframe");
And change:
document.body.insertBefore(iframe, document.body.firstChild);
To:
div.appendChild(iframe);
document.body.insertBefore(div, document.body.firstChild);
本文标签: JavaScript Center AlignStack Overflow
版权声明:本文标题:JavaScript Center Align - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742248083a2440231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论