1

我有一个输入框供用户输入任何网址。然后我想在他们点击提交后在 iframe 中显示网站。

<html>
    <head>
        <title></title>
        <style>
            #netframe {
                float:right; 
                height: 100%; 
                width: 80%;
            }

            #sidebar {
                float:left; 
                height: 100%; 
                width: 20%;
            }
        </style>
    </head>
    <body>
        <div id="container">
            <div id="sidebar">
                Enter A URL:
                <input type="text" name="url" id="url">
                <input type="button" value="load" id="load">
                <br><br>    
            </div>
            <div id="netframe">
                <iframe height="100%" width="100%" class="netframe" src="pullsite.php" id="main_frame"></iframe>
            </div>
        </div>
    </body>
</html>

这是我在网上找到的最接近的,但它没有做任何事情: How To Reload A iframe Using jQuery

4

2 回答 2

2

jsBin 演示

HTML:

<input type="text" id="url" name="url" value="http://" /> 
<iframe height="90%" width="100%" src="" id="frame"></iframe>

jQuery

$('input#url').on('input', function() {
  $('#frame').attr('src', this.value);
});
于 2012-09-25T00:44:48.197 回答
0

尝试将以下 JS / jQuery 脚本添加到您的代码中:

<script>
    $(function() {
        $("#load").click(function() {
            var new_url = $("#url").val();

            // Checks that the user typed "http://" or not
            if(new_url.substr(0,7)!="http://")
                new_url = "http://"+new_url;

            $("#main_frame").attr("src", new_url);
        });
     });
</script>

因此,您的代码将如下所示:

<html>
    <head>
        <title></title>
        <script type="text/javascript"  src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <style>
            #netframe {
                float:right; 
                height: 100%; 
                width: 80%;
            }

            #sidebar {
                float:left; 
                height: 100%; 
                width: 20%;
            }
        </style>
        <script>
            $(function() {
                $("#load").click(function() {
                    var new_url = $("#url").val();

                    // Checks that the user typed "http://" or not
                    if(new_url.substr(0,7)!="http://")
                        new_url = "http://"+new_url;

                    $("#main_frame").attr("src", new_url);
                });
             });
        </script>
    </head>
    <body>
        <div id="container">
            <div id="sidebar">
                Enter A URL:
                <input type="text" name="url" id="url">
                <input type="button" value="load" id="load">
                <br><br>    
            </div>
            <div id="netframe">
                <iframe height="100%" width="100%" class="netframe" src="pullsite.php" id="main_frame"></iframe>
            </div>
        </div>
    </body>
</html>

PS:不要忘记加载 jQuery 库。例如,您可以通过在标题中添加以下内容从 Web 加载它:

<script type="text/javascript"  src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

希望这可以帮助。让我知道这是否对你有用。

于 2012-09-25T01:34:22.530 回答