search

Home  >  Q&A  >  body text

javascript - Using load in jquery to load another html page, can it operate elements in another page?

Using load in jquery to load another html page, can I operate elements in another page?

淡淡烟草味淡淡烟草味2769 days ago663

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-05-19 10:38:43

    If you want to completely load a subpage in this page, it is not recommended to load it directly to the elements of this page;

    Do you think this solution would be better:

    The general approach is to first load this subpage in an iframe by modifying the src attribute (so that the subpage and the parent page have independent scopes, minimizing various conflicts);

    The next step is to obtain the window object in the iframe subpage. In the future, you can use the window object of this subpage to obtain the corresponding elements of the subpage and perform operations:

    <iframe src="动态替换这个属性即可"></iframe>
    // 获取iframe子页面的window对象
    var iframeWindow = document.getElementById('iframe的id').contentWindow;
    // 获取子页面的元素
    var element = iframeWindow.document.getElementById('子页面中某个元素的id');
    
    // jquery版
    var iframeWindow = $('iframe的选择器').prop('contentWindow');
    var element = $(iframeWindow).find('要获取元素的选择器');

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-19 10:38:43

    index is the page being loaded

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <p class="demo">
      我是load页面
    </p>
    </body>
    </html>

    test is the page that executes load

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <p id="p">
    
    </p>
    </body>
    <script src="jquery.js"></script>
    <script>
    $("#p").load("index.html",function () {
        console.log($(".demo").html())//打印出来
        $(".demo").html(
            "<span>load出来的页面 更改内容</span>" +
            "<span>哈哈哈</span>"
        )
    })
    
    </script>
    </html>

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-19 10:38:43

    You can operate as you like after loading.

    reply
    0
  • Cancelreply