简体   繁体   English

使用jQuery加载页面内容吗?

[英]Load the page content using jQuery?

Using jQuery load html forms dynamically using append function. 使用jQuery使用附加函数动态加载html表单。 Here the following code load the page content dynamically based on number times of values on while loop. 在这里,以下代码根据while循环中值的次数动态加载页面内容。

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; 在这里,我要用不同的值来加载内容。在var load_with_value = 0上,它只能使用0或1的单个值; but not on both simultaneously ie increment the load_with_value++ for again load the page content of HTML forms. 但不能同时使用这两种方法,即增加load_with_value ++以再次加载HTML表单的页面内容。

$(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++;
        }
    });
});

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) . 然后,您可以使用$('#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") . 如果要将内容加载到现有元素中,则应使用其ID,类或其他选择器来执行此操作-而不是$("<div>") ..如果要将其加载到所有div元素中(请不要t)则应为$("div")

Next var n = $('.item').length + 1; 下一个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. 如果不需要,请不要使用while循环。 You can use: 您可以使用:

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

What is var load_with_value = 0; 什么是var load_with_value = 0; used for? 用于? I can only see you incrementing it with load_with_value++; 我只能看到您使用load_with_value++;递增它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 最后,如果要基于增量变量加载不同的内容,则应在.load函数之外完成。

$(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. 这会将内容/somecontent-0.html加载到/somecontent-4.html到ID分别为container-0container-4容器元素中。

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

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