【问题标题】:Set default content for target div为目标 div 设置默认内容
【发布时间】:2017-01-09 15:37:33
【问题描述】:

如果这是我的代码,我如何设置 page1.html 在页面加载时默认显示?

提前致谢。

<head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
    function swapContent(targetID, url) {
        var target = document.getElementById(targetID);
 
        var ajax = new XMLHttpRequest();
        ajax.open('GET', url, true);
        ajax.onreadystatechange = function() {
            if(ajax.readyState == 4) {
                target.innerHTML = ajax.responseText;
            }
        }
        ajax.send(null);
    }
    </script>
</head>
<body>
    <div>
        <div>
            <a href="javascript: void();" onclick="swapContent('searchDiv', 'page1.html');">First</a> |
            <a href="javascript: void();" onclick="swapContent('searchDiv', 'page2.html');">Second</a> |
            <a href="javascript: void();" onclick="swapContent('searchDiv', 'page3.html');">Third</a>
        </div>
        <hr>
        <div id="searchDiv" style="width: 550px; height: 400px; overflow: scroll;">Please select one...</div>
    </div>
</body>
</html>

【问题讨论】:

    标签: javascript html css ajax


    【解决方案1】:

    如果你想让它被javascript加载:

    <body onload="swapContent('searchDiv', 'page1.html');"> 
    

    【讨论】:

      【解决方案2】:

      您可以将 onload 事件处理程序添加到 body 元素:

       <body onload ="swapContent('searchDiv', 'page1.html');">
          ....
      
       </body>
      

      【讨论】:

        【解决方案3】:

        body标签的onload中调用swapContent('searchDiv', 'page1.html');怎么样?

        【讨论】:

          【解决方案4】:

          要么把它放在 onload 事件中,要么根据你的框架,你可以在 django 的 div 中包含 page1

          <div>{% include 'page1.html' %}</div>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-09-02
            • 1970-01-01
            • 2016-04-25
            • 2018-12-03
            • 1970-01-01
            • 2020-02-01
            • 2018-09-02
            • 1970-01-01
            相关资源
            最近更新 更多