最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to dynamically adjust html table based on browser window size - Stack Overflow

programmeradmin4浏览0评论

I want my site to display differently based on the size of the web browser. Based on the questions/answers from other users, I got this to work with JavaScript onload. But I can't get it to work dynamically -- that is to say, in real-time, as users are adjusting the window. They have to refresh to get the format of the webpage to change.

Here is my current code:

<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="&nbsp;First name">
                        </td>
                        <td>
                            <input type="text" name="search" class="lastname" value="&nbsp;Last name">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="text" name="search" class="email" value="&nbsp;Email">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div style="color:#979797;">Password:&nbsp;<br /><input type="password" name="password" class="password" value="Password"></div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div style="color:#979797;">Verify password:&nbsp;<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 -->


Note: I know I shouldn't be used tables, I should be using divs. But that's another issue... I started building this website before I knew that. I also know it's pretty sloppy. So just bear with me. I'm trying to just go with it until I have time to fix it up.

I want my site to display differently based on the size of the web browser. Based on the questions/answers from other users, I got this to work with JavaScript onload. But I can't get it to work dynamically -- that is to say, in real-time, as users are adjusting the window. They have to refresh to get the format of the webpage to change.

Here is my current code:

<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="&nbsp;First name">
                        </td>
                        <td>
                            <input type="text" name="search" class="lastname" value="&nbsp;Last name">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="text" name="search" class="email" value="&nbsp;Email">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div style="color:#979797;">Password:&nbsp;<br /><input type="password" name="password" class="password" value="Password"></div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div style="color:#979797;">Verify password:&nbsp;<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 -->


Note: I know I shouldn't be used tables, I should be using divs. But that's another issue... I started building this website before I knew that. I also know it's pretty sloppy. So just bear with me. I'm trying to just go with it until I have time to fix it up.

Share Improve this question edited Sep 1, 2014 at 0:49 Moshe Katz 16.9k7 gold badges70 silver badges125 bronze badges asked Sep 1, 2014 at 0:39 John SteveJohn Steve 1011 gold badge3 silver badges8 bronze badges 4
  • 1 $(window).resize(function(){ ... }); – Oliboy50 Commented Sep 1, 2014 at 0:46
  • Don't feel bad, tables and iframes are not dead yet! And won't be in a closer future. – Guillermo Gutiérrez Commented Sep 1, 2014 at 0:48
  • @GuillermoGutiérrez Not sure what future you're looking at - they'll be dead in most new work pretty soon. – Moshe Katz Commented Sep 1, 2014 at 0:50
  • developer.mozilla/en-US/docs/Web/API/Window.onresize – Moshe Katz Commented Sep 1, 2014 at 0:51
Add a ment  | 

2 Answers 2

Reset to default 5

You shouldn't use JavaScript to make responsive web design. Use CSS instead, with media querys. So you write your 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="&nbsp;First name">
                        </td>
                        <td>
                            <input type="text" name="search" class="lastname" value="&nbsp;Last name">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="text" name="search" class="email" value="&nbsp;Email">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <div style="color:#979797;">Password:&nbsp;<br /><input type="password" name="password" class="password" value="Password"></div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div style="color:#979797;">Verify password:&nbsp;<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>

Use the following CSS:

@media all and (max-width: 899px) {
    #big_device_index {
        display: none;
    }
@media all and (min-width: 900px) {
    #small_device_index {
        display: none;
    }

You can find out more about media queries here }

Add this outside of 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>');
        }
}

hope this will help you, make sure to include fisrt jquery.min.js

发布评论

评论列表(0)

  1. 暂无评论