2011-10-02 142 views
0

如何将整个脚本对齐到中心位置?我从来没有真正用JavaScript编码过,我只是想修改一个快速书签。我已经尝试过像align = center这样的脚本的不同组件,但没有运气。是否有像<center>(在html中)为js?JavaScript中心对齐

感谢

编辑:基本上,我想这是一个页面

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

请给我们更多的细节 - 你的术语是混杂起来的,很难理解你要做什么。 (特别是请不要像你这样使用文字脚本。) – Ariel

+0

我编辑了我的文章 – Cody

+0

查看下面我编辑的答案,我解决了你的问题。 –

回答

1

不能居中一个脚本中心,它看起来并不像什么所以没什么中心。

如果脚本生成了一些HTML,则可以将生成的元素居中。这应该使用the usual CSS techniques应用于生成的元素。

<center>align属性为deprecated in 1998,不应使用)。

1

尝试改变:

iframe.style.margin= "0"; 

要:

iframe.style.margin= "auto"; 

如果它不工作,让我知道在评论。

OK,试试这个变化而是改变:

var iframe = document.createElement("iframe"); 

到:

var div = document.createElement("div"); 
div.style.width = w+'px'; 
div.style.margin= "auto"; 
var iframe = document.createElement("iframe"); 

而变化:

document.body.insertBefore(iframe, document.body.firstChild); 

要:

div.appendChild(iframe); 
document.body.insertBefore(div, document.body.firstChild); 
+0

没有工作:(仍然对齐到左边 – Cody

+0

我更新了我的评论,请尝试新的更改 – Ariel

+0

现在整个书签不会加载:(. – Cody