如何根据浏览器窗口大小动态调整html表
How to dynamically adjust html table based on browser window size
我希望我的网站根据web浏览器的大小进行不同的显示。基于其他用户的问题/答案,我将其用于JavaScript onload
。但我无法让它动态工作——也就是说,当用户调整窗口时,它是实时的。他们必须刷新才能更改网页的格式。
这是我当前的代码:
<table class="index_table" border="0">
<tr>
<!--BELOW IS JAVASCRIPT FOR ADJUSTING HTML BASED ON WINDOW SIZE -->
<script>
if (window.innerWidth > 900){
document.write('<td rowspan="3" class="index_article"></td>');
}
</script>
<!-- END OF JAVASCRIPT -->
<td class="index_article_light">
<h2 class="index_instructions_subheader">INSERT HEADER HERE</h2>
<p class="index_instructions">INSERT PARAGRAPH HERE.</p>
<p class="index_instructions">INSERT PARAGRAPH HERE. </p>
<br />
<form action="signup.html" style="vertical-align:top;">
<table border="0" style="margin-left:auto;margin-right:auto;width:400px;">
<tr>
<td>
<input type="text" name="search" class="firstname" value=" First name">
</td>
<td>
<input type="text" name="search" class="lastname" value=" Last name">
</td>
</tr>
<tr>
<td colspan="2">
<input type="text" name="search" class="email" value=" Email">
</td>
</tr>
<tr>
<td colspan="2">
<div style="color:#979797;">Password: <br /><input type="password" name="password" class="password" value="Password"></div>
</td>
</tr>
<tr>
<td>
<div style="color:#979797;">Verify password: <input type="password" name="verify_passwords" class="password" value="Password"></div>
</td>
</tr>
<tr>
<td colspan="2">
<select>
<option value="kent">INSERT LIST HERE</option>
</select>
</tr>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="signup!" style="float:right;" id="result_submit">
</td>
</tr>
</table>
</form>
</td>
</tr>
<!--BELOW IS JAVASCRIPT FOR ADJUSTING HTML BASED ON WINDOW SIZE -->
<script>
if (window.innerWidth < 900){
document.write('<tr><td class="index_article" style="height:200px;"></td></tr>');
}
</script>
<!-- END OF JAVASCRIPT -->
注意:我知道我不应该使用表,我应该使用div
s。但这是另一个问题。。。在我意识到这一点之前,我就开始建立这个网站。我也知道这很草率。所以请耐心等待。我正努力坚持下去,直到我有时间把它修好。
您不应该使用JavaScript进行响应式web设计。使用CSS代替媒体查询。所以你写你的HTML:
<table class="index_table" border="0">
<tr>
<!--This only will display on screen-width>900-->
<td id="big_device_index" rowspan="3" class="index_article"></td>
<td class="index_article_light">
<h2 class="index_instructions_subheader">INSERT HEADER HERE</h2>
<p class="index_instructions">INSERT PARAGRAPH HERE.</p>
<p class="index_instructions">INSERT PARAGRAPH HERE. </p>
<br />
<form action="signup.html" style="vertical-align:top;">
<table border="0" style="margin-left:auto;margin-right:auto;width:400px;">
<tr>
<td>
<input type="text" name="search" class="firstname" value=" First name">
</td>
<td>
<input type="text" name="search" class="lastname" value=" Last name">
</td>
</tr>
<tr>
<td colspan="2">
<input type="text" name="search" class="email" value=" Email">
</td>
</tr>
<tr>
<td colspan="2">
<div style="color:#979797;">Password: <br /><input type="password" name="password" class="password" value="Password"></div>
</td>
</tr>
<tr>
<td>
<div style="color:#979797;">Verify password: <input type="password" name="verify_passwords" class="password" value="Password"></div>
</td>
</tr>
<tr>
<td colspan="2">
<select>
<option value="kent">INSERT LIST HERE</option>
</select>
</tr>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="signup!" style="float:right;" id="result_submit">
</td>
</tr>
</table>
</form>
</td>
</tr>
<!--This only will display on screen-width<900-->
<tr id="small_device_index"><td class="index_article" style="height:200px;"></td></tr>
使用以下CSS:
@media all and (max-width: 899px) {
#big_device_index {
display: none;
}
@media all and (min-width: 900px) {
#small_device_index {
display: none;
}
您可以在此处了解有关媒体查询的更多信息}
将此添加到if (window.innerWidth > 900){.....
之外
$(window).resize(function(){
if (window.innerWidth > 900){
$('.index_article').remove();
$('.index_table).find('tr:first').append('<td rowspan="3" class="index_article"></td>');
}
}
希望这将对您有所帮助,请确保包含fisrtjquery.min.js
相关文章:
- 加载图像后调整HTML画布的大小
- 根据窗口大小调整 HTML 中 SVG 图像的大小
- 使用src调整HTML Iframe的大小我可以't控制
- 根据屏幕大小自动调整HTML按钮的大小
- 如何在手机上调整 html 页面的大小
- 如何动态调整html文本框的大小以避免溢出
- 网页大小自动调整HTML简单
- D3和Select未正确调整Html布局
- 使用javascript调整HTML多个全宽视频的大小
- 使用jQuery调整HTML表列的大小
- 如何调整html<画布>动态使用Javascript
- 如何调整HTML元素的高度以覆盖其所有可用空间'的父级
- 使用javascript调整html画布的大小
- 如何根据浏览器窗口大小动态调整html表
- HTML标签调整HTML元素的大小
- 使用触摸调整HTML元素的大小
- 用javascript调整html表td的大小
- 调整html表列的大小
- 使用Jquery调整Html表的行和列大小
- 如何使用jquery重新调整html表的行和列的大小