douji1877 2016-02-02 06:01
浏览 20
已采纳

使用jQuery加载页面内容?

Using jQuery load html forms dynamically using append function. Here the following code load the page content dynamically based on number times of values on while loop.

Here I have a struggle on load the content with different values.its working with single value of 0 or 1 on var load_with_value=0; but not on both simultaneously i.e. increment the load_with_value++ for again load the page content of HTML forms.

$(document).ready(function(e) {
    $("<DIV>").load("<?php echo $url; ?>", function() //url for loading page
    {
        var n = $('.item').length + 1;           //load the html page content 
        var i = 1;                              //iteration for number of times load the content
        var count = 2;                         //check the condition
        var load_with_value = 0;              //load the page content with different values for display different values on html form  

        while(i<count) {                      //loop starts
            $("#product").append($(this).html());
            i++;
            load_with_value++;
        }
    });
});
  • 写回答

1条回答 默认 最新

  • drkjzk3359 2016-02-02 07:57
    关注

    First of all let's do some proper code formatting and get rid of the incorrect comments:

    $(document).ready(function(e) {
        $("<DIV>").load("<?php echo $url; ?>", function() {
            var n = $('.item').length + 1;           
            var i = 1;                              
            var count = 2; 
            var load_with_value = 0;           
            while(i<count) {
                $("#product").append($(this).html());
                i++;
                load_with_value++;
            }
        });
    });
    

    Now let's take it apart:

    If you want to use a temporary element to store the loaded data you need to assign it to a variable, so instead of

    $("<DIV>").load("<?php echo $url; ?>", function() {
    

    do

    var tempObject = $("<div/>").load("<?php echo $url; ?>", function() {
    

    Afterwards you can append the temporary element to an existing one with $('#someExistingElement').append(tempObject).

    If you want to load the content into an existing element you should use it's ID, class or other selector to do this - not $("<div>").. If you want to load it to all div elements (please don't) then it should be $("div").

    Next var n = $('.item').length + 1; makes no sense. It is never used in the code.

    While cycle in this case is unnecessary. Don't use while cycles if you don't have to. You can use:

    for(var i=0; i<count; i++){
        //code
    }
    

    What is var load_with_value = 0; used for? I can only see you incrementing it with load_with_value++; but you don't use it anywhere..

    Finally if you want to load different content based on the incremented variable it should be done outside of the .load function.. For example

    $(document).ready(function(){
        for(var i=0; i<5; i++){
            $('#container-' + i).load('/somecontent-' + i + '.html');
        }
    });
    

    This loads the content /somecontent-0.html to /somecontent-4.html into container elements with IDs container-0 to container-4 respectively.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。