简体   繁体   English

如何将html元素填充到另一个页面?

[英]How can I populate html elements into another page?

This is my jQuery function in script.js This function is use to fetch the feed from Flicker and populate the structure. 这是我在script.js中的jQuery函数。此函数用于从Flicker获取feed并填充结构。

I want to populate html structure into another html page called "items.html" 我想将html结构填充到另一个名为“items.html”的html页面中

$function getImages(){
    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=90528678@N03&lang=en-us&format=json&jsoncallback=?", displayImages);   

  function displayImages(data) {      

// Start putting together the HTML string
var htmlString = '';         

// Now start cycling through our array of Flickr photo details
    $.each(data.items, function(i,item){
    // Here's where we piece together the HTML
    htmlString += '<div class="item_wrapper" name="'+item.title+'">';
    htmlString += '<img class="item_picture" src="' + item.media.m + '" alt=""/>';
    htmlString += '<div class="item_data">';
    htmlString += '<div class="item_company">';
    htmlString += '<h3 class="fi se en">'+item.title+'</h3>';
    htmlString += '</div>';
    htmlString += '<div class="item_title">';
    htmlString += '<h3 class="fi">'+item.title+'</h3>';
    htmlString += '<h3 class="se">'+item.title+'</h3>';
    htmlString += '<h3 class="en">'+item.title+'</h3>';
    htmlString += '</div>';
    htmlString += '</div>';
    htmlString += '</div>';
});   
     // Pop our HTML in the DIV tag of items.html 
     // Here's the problem. 
    $('div').html(htmlString);
   }
}

So how can I do it? 那我该怎么办呢? Thanks !!! 谢谢 !!!

您可以使用ajax加载函数来加载网页中的html内容。

$('div').load("file.html");

You can load another html page into an element using: 您可以使用以下命令将另一个html页面加载到元素中:

$('div').load("items.html");

You also shouldn't use string concatenation to build your dom elements. 您也不应该使用字符串连接来构建dom元素。

Use something like this: 使用这样的东西:

var div = $('<div/>');

$.each(data.items, function(i, image) {

    var item = $('<div/>').addClass("item_wrapper").attr('name', image.title);

    $('<img/>').attr('src', image.media.m).appendTo(item);

    item.appendTo(div);

});

$('div').html(div);

and so on... 等等...

Here is a simplified version that does work. 这是一个有效的简化版本。

getImages();
function getImages(){
    $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=90528678@N03&lang=en-us&format=json&jsoncallback=?",
    function(data) {
        var htmlString = '';
        $.each(data.items, function(key, value){
                htmlString += '<div>'+value.title+'</div>';
                htmlString += '<img src="'+value.media.m+'">';
        });
        $('div').html(htmlString);
    });  
}

I actually have no idea why your version didn't work. 我实际上不知道为什么你的版本不起作用。

It could be possible that the callback for getJSON needs to be an anonymous no name function, and that's why the displayImages function isn't working. 有可能getJSON的回调需要是匿名无名称函数,这就是displayImages函数无法正常工作的原因。 I couldn't get it to work. 我无法让它发挥作用。

You do have a $ in front of your function declaration. 你的函数声明前面有一个$。 Maybe that contributed to the problem. 也许这导致了这个问题。

Though the html string insert works, like Josh said DOM insertion with nodes is better than html insertion. 虽然html字符串插入工作,但像Josh说,DOM插入节点比html插入更好。 If you're just looking for a quick and dirty solution html string insertion might be fine. 如果你只是在寻找一个快速而肮脏的解决方案html字符串插入可能没问题。

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

相关问题 如何通过单击另一个html页面上的按钮来编辑页面? - How can I edit a page by clicking a button on the another html page? 如何将HTML元素复制到另一个Div? - How can I copy a HTML elements to another Div? 如何在页面上的多个元素之间共享javascript / html? - How can I share javascript/html among several elements on a page? 如何用HTML元素填充数组 - How to populate an array with HTML elements 在JQuery应用功能之前,如何用AJAX生成的HTML填充页面 - How can I populate a page with AJAX generated HTML before JQuery applies functionality 如何将div替换为另一个div(但外部html页面)? - How Can I replace a div with another div(but of external html page)? 如何使用JQuery从外部html页面中的内容填充多个div,并根据每个div上的属性更新该内容? - How can I use JQuery to populate multiple divs with content from external html page and update that content based on attributes on each div? 如何使用第一个下拉列表中的onChange事件填充另一个下拉列表? - How can I populate another dropdown with the onChange event of the first dropdown? 如何让一个 html 页面上的事件更改影响另一个使用 jquery 具有条件的页面? - How can I get the event change on one html page influence another page that has conditionals using jquery? 如何用javascript数组填充html标题? - How can I populate html titles with a javascript array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM