【问题标题】:How do I set a CSS width from a javascript function result?如何从 javascript 函数结果中设置 CSS 宽度?
【发布时间】:2010-11-18 23:23:35
【问题描述】:

我在 div 包装器中有一个 GridView,左侧有一些行标题需要始终可见。到目前为止,这一切都有效,但我需要包装器具有可变宽度以适应浏览器大小。

我使用一些 javascript 根据浏览器宽度获得了所需的包装器宽度,但我不知道如何将此宽度设置为 wrapper.width。
它不必在页面加载后更新包装器宽度或检查浏览器调整大小。

我在图表方面的糟糕尝试:

|   |column headers      |
| R |--------------|      
| O | gridview data      |
| W |              |      
|   |  this part         |
| H |  will scroll |<--->
| E |  while the         |
| A |  Row headers |
| D |  stay there        |
| E |              |
| R |______________      |
| S | scroll bar   |

asp:(见下面的编辑)

<pseudocode>
 <table>
   <tr><td>row headers</td>
       <td><div class="Wrapper">
              <asp:GridView>dataBind()</asp:gridview>
       </div></td></tr>
 </table>
</pseudocode>

css:

div.Wrapper 
{
 width:800px;<--this needs to change based on the browser width
 overflow: auto;
}

javascript:

var Width = window.innerWidth - 275 || document.body.clientWidth - 275

我要么需要一种设置 wrapper.width = Width 的方法,要么需要一种完全不同且希望更好的方法来实现这一点。

我尝试使用 % 作为宽度,但它没有将浏览器窗口用作 100%,它占用了整个 GridView 的整个宽度的 %,这对我没有任何作用。

提前感谢您的帮助。

编辑:添加了一些代码

<script type="text/javascript">
    var Width = window.innerWidth - 275 || document.body.clientWidth - 275;
    var divElement = document.getElementById("wrappist");
<!--I tried a few different things at this point, here are a couple of them-->
    divElement.offsetWidth = "80px";
    divElement.style.width = Width.toString() + 'px'; 
</script>

<div runat="server" id="wrappist" class="Wrapper">
  <asp:GridView runat="server" ID="ALPGrid" CssClass="Grid"
       CellPadding="5" GridLines="Both" AutoGenerateColumns="false"
       OnRowDataBound="ALP_RowDataBound" OnRowCreated="ALP_RowCreated"
       DataKeyNames="ItemID">
  <HeaderStyle CssClass="GridHeader" />
  <RowStyle CssClass="Row" />
  <columns>column stuff in here</columns>
  </asp:GridView>
</div>

【问题讨论】:

  • 我会在周一重新开始工作时查看更多解决方案。希望我已经发布了足够的信息来获得更多帮助。再次感谢您迄今为止的建议。

标签: javascript asp.net css


【解决方案1】:

试试这个:

var Width = window.innerWidth - 275 || document.body.clientWidth - 275
var divElement = document.getElementById('idOfDiv');
d.style.width = Width.toString() + 'px';

编辑:忘记解释了。这会设置元素上 div 内联的宽度,因此它将覆盖您的类中的样式。

【讨论】:

  • 我无法让它工作,我一直在阅读 object.style.width 但它没有为我做任何事情。我正在 FireFox 中测试它。
  • 写道,如果不针对特定示例进行测试,您能否公开一个尝试并实现此功能的页面,以便我看一下?
  • 我在原始问题的底部添加了一些代码。该页面位于无法在站点外访问的 Intranet 上,因此我无法链接它。
  • 我得到了一些其他的帮助,但这是正确的。我不得不去掉
    标签上的 runat="server"
【解决方案2】:

纯 CSS 解决方案怎么样?

<div class="wrapper">
<div class="row_headers"></div>
<div class="grid"></div>
</div>

CSS

.wrapper
{
width:800px; /*remove/change */
}
.row_headers
{
float:left;
width:100px; /*keep same as .grid margin-left */
height:300px;
background-color:#333;
padding-bottom:16px;
}
.grid
{
margin-left:100px; /*keep same as .row_headers width */
height:300px;
padding-bottom:16px;
background-color:#ddd;
overflow:auto;
}

【讨论】:

  • 感谢您的意见,我认为这会比我这样做的方式更干净。不幸的是,它给我留下了同样的问题。我不知道如何将页面加载时的 800 更改为浏览器宽度。
  • 800 像素?包装宽度?你不必改变任何东西。只需删除该值,它将与其父元素一样宽,因此如果父元素是主体,它将与主体一样宽。除非您设置了不同的宽度,否则正文将与浏览器一样宽。
猜你喜欢
  • 2015-11-13
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 2019-03-09
  • 2016-05-30
  • 1970-01-01
相关资源
最近更新 更多