简体   繁体   English

使用jQuery将网页加载到div中

[英]load web page into div using jquery

I need help. 我需要帮助。 In my web page, I want that in the div "pagina", would open the link www.google.it when I click on the link without that the page executes the refresh. 在我的网页中,我希望当我单击该链接时,在“ pagina” div中打开链接www.google.it时,页面不会执行刷新。 I've tried doing it this way but it does not work. 我已经尝试过以这种方式进行操作,但无法正常工作。

<html>
<head>

        <title>Home Page</title>

    <link rel="stylesheet" href="../jquery-ui-1.11.4.custom/jquery-ui.css">
    <script src="../jquery-ui-1.11.4.custom/external/jquery/jquery.js"></script>
    <script src="../jquery-ui-1.11.4.custom/jquery-ui.js"></script>
    <script type="text/javascript">

    $("a").click(function()
    {
        $("#pagina").load($(this).attr("href"));

        return false;
    });     
    </script>
</head> 

<body class="body">

    <div id="header">
        <table border="0" cellspacing="5" cellpadding="5" align="right">
            <tr>
                <form name="ricerca" method="POST" >
                    <td><input type="text" name="nome" size="35" placeholder="Cerca Titolo, Attore, Regista, Anno"></td> <!--ENTER -->
                    <td> <input class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="submit" value="Accedi"></td>
                </form>
            </tr>
        </table>
    </div>
    <br>
    <div class="nav">
        <table>
            <tr>
                <td> Categorie:             
                <ul id="menu">
                    <li><a href="https://www.google.it">Animazione</a></li>
                    <li>Avventura</li>
                    <li>Azione</li>
                    <li>Commedia</li>
                    <li>Documentario</li>
                    <li>Drammatico</li>
                    <li>Erotico</li>
                    <li>Fantascienza</li>
                    <li>Horror</li>
                    <li>Musical</li>
                    <li>Romantico</li>
                    <li>Thriller</li>
                    <li>Western</li>
                </ul>
                </td>
            </tr>
        </table>
    </div>
    <div class="pagina">

    </div>

</body> 
</html>

could you use an <iframe></iframe> tag?, or object tag? 您可以使用<iframe></iframe>标签吗?还是使用对象标签? try looking here http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe 尝试在这里查看http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe

UPDATE: You would have to use an <iframe> to do that since it is not in the same domain. 更新:您必须使用<iframe>来执行此操作,因为它不在同一域中。

You need to prevent the default action of anchor links when they are clicked by calling the event.preventDefault() method before performing the load operation. 在执行加载操作之前,您需要通过调用event.preventDefault()方法来防止单击锚链接时的默认操作。

$("a").click(function(event)
{
    event.preventDefault();
    $("#pagina").load($(this).attr("href"));

    // return false;
}); 

hope it's work for you: 希望它为您工作:

  $("a").click(function(event){
      event.preventDefault();
      var url = $(this).attr('href');
      var iframe = '<iframe src="'+url+'"></iframe>';
      $("#pagina").html(iframe);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM