javascript 如何使用javascript调整div宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12768948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resize div width with javascript
提问by Zsolt Janes
I would like to resize the pagewrapper div with javascript. I have chrome and would like to use it as userscript. Here is the site link: http://clanbase.ggl.com. I want to take the pagewrapper to 100% width. I have the following code:
我想用 javascript 调整 pagewrapper div 的大小。我有 chrome 并想将它用作用户脚本。这是网站链接:http: //clanbase.ggl.com。我想将 pagewrapper 设置为 100% 宽度。我有以下代码:
// ==UserScript==
// @match http://clanbase.ggl.com/*
// ==/UserScript==
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
function pagewrapper() {
document.getElementById('pagewrapper').style.Width = "100%";
}
addJQuery(pagewrapper);
回答by Jeffrey Sweeney
One thing to note is that non-absolutely positioned divider elements automatically take up 100% of the width, so you might not need to do anything. Just try removing the width property from the pagewrapper
element (in the CSS) and see if it works. You could also try overriding the width property:
需要注意的一件事是,非绝对定位的分隔元素会自动占据 100% 的宽度,因此您可能不需要做任何事情。只需尝试从pagewrapper
元素中删除 width 属性(在 CSS 中),看看它是否有效。您还可以尝试覆盖 width 属性:
#pagewrapper {
width:100%;
}
If setting CSS isn't an option for whatever reason, this script should suffice:
如果出于某种原因无法设置 CSS,则此脚本应该足够了:
function pagewrapper() {
document.getElementById('pagewrapper').style.width = "100%";
}