开发者

JavaScript Center Align

开发者 https://www.devze.com 2023-04-10 05:40 出处:网络
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 components

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 components of the script but no luck. Is there something like <center> (in html) for js?

Thanks

EDIT: basically i want this 开发者_如何学JAVAto be centered on a page

function dEmbed(){
    var iframe = document.createElement("iframe");
    iframe.src = 'http://www.google.com';
    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);
}


I was able to achieve this by injecting the iframe into an existing div with text-align: center.

Here is the example: http://jsfiddle.net/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 comment.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号