简体   繁体   English

通过类名称从jQuery获取外部文件的div

[英]Get divs from an external file by class name.with jQuery

Sorry if this has been answered before but I searched for hours trying to find how to do what I am trying to do. 很抱歉,如果之前已经回答过,但我搜索了几个小时试图找到我想要做的事情。 I know php would probably have an easier solution but php might not an option. 我知道php可能有一个更简单的解决方案,但PHP可能不是一个选项。 Jquery is preferable although pure javascript would be great too. Jquery是优选的,虽然纯javascript也会很棒。

I'm trying to get divs from an external file by class name. 我试图通过类名从外部文件中获取div。 There are multiple divs using the same class name. 有多个div使用相同的类名。 I want to get all the div of that class put in an array that I can loop through in a later part of the script. 我希望将该类的所有div放入一个数组中,我可以在脚本的后续部分循环。

I'm trying keep the contents of the divs of this page synced up to the divs of a page of an external html file that will always be changing. 我正在尝试将此页面的div的内容同步到外部html文件的页面的div,该文件将始终在更改。 But I'm not sure how best to do this. 但我不确定如何最好地做到这一点。

I have had good results getting data from csv files into divs in other projects, no problem, but this one has me stymied. 我有很好的结果将csv文件中的数据转换成其他项目中的div,没问题,但这个让我受阻。

I tried the following (for testing purposes, I was just logging to console). 我尝试了以下(为了测试目的,我只是登录到控制台)。 I have confirmed that the external file loads just fine. 我已经确认外部文件加载得很好。 I can see it load in the Chrome Dev Tools. 我可以在Chrome开发工具中看到它的负载。 But the data never really seems to go anywhere after that. 但是之后数据似乎从未真正发生过。

$(document).ready( function() {
    var array = [];
    var testfunc =  $('.test').each(function () {array.push(this.innerHTML);});

    $.get('sharedResources/Bio.html', function(testfunc){
            console.log(array);
                                     });

});

I tried other variations but they were even messier and the script started pulling from classes of the current page instead of the external file. 我尝试了其他变体,但它们甚至更加混乱,脚本开始从当前页面的类而不是外部文件中提取。 I could see the innerHTML of the current page showing up in the console log. 我可以看到当前页面的innerHTML显示在控制台日志中。

I'm sure there is a much better way that I'm not seeing. 我确信有一种更好的方式,我没有看到。

I also tried something else I saw suggested online but this didn't works either. 我也尝试过我在网上看到的其他建议,但这也不起作用。 This gave me 'contents is undefined' error... not to surprised at that one. 这给了我“内容未定义”的错误......不要对那个感到惊讶。

$.ajax('sharedResources/Bio.html').done(function(e) {
    $('.test').attr('innerHTML', contents);
});     

Sorry if I am not asking this well. 对不起,如果我不是这么问。 I don't often ask anything on these forums. 我经常在这些论坛上问什么。

Thanks! 谢谢!

Assuming the URL is within the same origin, all you really have to do is 假设URL在同一个源中,您真正需要做的就是

$.get('sharedResources/Bio.html', function(html){
    var elements = $('<div />', {html: html}).find('.className');
}).fail(console.log);

And note that elements would only be available within that callback functions scope, as it's asynchronous. 请注意, elements 只能在该回调函数范围内使用,因为它是异步的。

Also note the added fail handler, and make sure you open the console and check for errors. 还要注意添加的fail处理程序,并确保打开控制台并检查错误。

I thought it might be useful to someone else searching for this sort of thing to post what I ended up doing. 我认为对于其他寻找此类事情的人发布我最终做的事情可能会有用。

I used what I learned from adeneo's great suggestion (above) and was totally able to do what I needed to do. 我使用了我从adeneo的伟大建议(上文)中学到的东西,并完全能够做我需要做的事情。 Thanks, adeneo 谢谢,adeneo

I needed to take team profile info from a bio page that often changes on the site and reuse & rearrange it on another page which needed to always be synced up. 我需要从生物页面中获取团队配置文件信息,这些信息通常会在网站上发生变化,并在另一个需要始终同步的页面上重复使用并重新排列。 So here is the code I ended up going with: 所以这是我最终得到的代码:

$.get('Bios.html', function(html){
    var t_name = $('<div />', {html: html}).find('.t_name');
    var t_creds = $('<div />', {html: html}).find('.t_creds');
    var t_title = $('<div />', {html: html}).find('.t_title');
    var t_bio = $('<div />', {html: html}).find('.t_bio');
    var t_img = $('<img />', {html: html}).find('.t_img');
    var t_imgTn = $('<img />', {html: html}).find('.t_imgTn');

            var arrayLength = t_name.length;

            for (var i = 0; i < arrayLength; i++) {
                $('#name'+[i]).html(t_name[i]);
                $('#creds'+[i]).html(t_creds[i]);
                $('#title'+[i]).html(t_title[i]);
                $('#bio'+[i]).html(t_bio[i]);
                $('#bioImg'+[i]).html(t_img[i]);
                $('#bioTn'+[i]).html(t_imgTn[i]);
                }
            }).fail(console.log);

});

It worked like a charm :-) 它像一个魅力:-)

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

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