简体   繁体   English

我是否正确使用jQuery .each函数?

[英]Am I using the jQuery .each function incorrectly?

I am using some jQuery to retrieve and parse some JSON from a third party website, so I can use images from that site to populate a gallery. 我正在使用一些jQuery从第三方网站检索和解析一些JSON,因此我可以使用该网站中的图像填充画廊。

Please see the details below, and reproduced in this JSFiddle : 请参阅下面的详细信息,并在此JSFiddle中转载

  $(document).ready(function() { function populateImages(path) { $('.crush').each(function() { $(this).html('<img src="' + path + '/>'); }); } $.ajax({ url: 'http://tubecrush.net/wp_api/v1/posts?callback=show', dataType: "jsonp", type: "get", success: function(data) { var regex = /"/g; var i = 0; for (i = 0; i < 20; i++) { var content = data.posts[i].content_display; var part1 = content.split('src="'); var part2 = part1[1].split(regex); var imgPath = part2[0]; console.log(imgPath); populateImages(imgPath); } } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <h1>Result:</h1> <div id="result"> <div class="pure-g"> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> <div class="crush"></div> </div> </div> 

I know the part that's retrieving the JSON query is working because it is outputting the image path I want to grab in the console. 我知道检索JSON查询的部分正在工作,因为它正在输出要在控制台中捕获的图像路径。 However, the last part is using the jQuery .each function to add an tag and fill it with the path I have retrieved with the AJAX, but it's not working. 但是,最后一部分是使用jQuery .each函数添加标签,并用我用AJAX检索到的路径填充它,但是它不起作用。

Not sure what I am doing wrong. 不知道我在做什么错。

Please note that although the second part is in its own function called populateImages , I have tried embedding that code in the loop part, and still had the same effect. 请注意,尽管第二部分在其自己的名为populateImages的函数中,但是我尝试将代码嵌入循环部分中,并且仍然具有相同的效果。

Greatly appreciated if anyone can help. 非常感谢任何人都可以提供帮助。

You shouldn't use each for that, it only means that you will put an image in every element for the first URL, then replace all of them with an image for the second URL and so on. 您不应该为此使用each ,这仅意味着您将在第一个URL的每个元素中放置一个图像,然后将所有图像替换为第二个URL的图像,依此类推。 You can make the populateImages function take a index where you want to place the image. 您可以使populateImages函数在要放置图像的位置获取索引。 Use the eq method to get a specific element: 使用eq方法获取特定元素:

function populateImages(index, path) {
  $('.crush').eq(index).html('<img src="' + path + '"/>');
}

Call the function with the loop counter as index: 以循环计数器为索引调用该函数:

populateImages(i, imgPath);

Everytime the loop does an iteration, you are populating every .crush with the same image. 每次循环执行一次迭代时, .crush在每个.crush相同的图像。 This should work: 这应该工作:

 $(document).ready(function(){
     var crushes = $('.crush');

     $.ajax({
         url:'http://tubecrush.net/wp_api/v1/posts?callback=show',
         dataType:"jsonp",
         type:"get",
         success:function(data){
             var regex = /"/g;
             var i = 0, l = crushes.length;
             for(i=0;i<l;i++) {
                 var content = data.posts[i].content_display;
                 var part1 = content.split('src="');
                 var part2 = part1[1].split(regex);
                 var imgPath = part2[0];
                 console.log(imgPath);
                 crushes.eq(i).html('<img src="' + imgPath + '"/>');
             } 
         }
     });
 });

You can easily narrow down your selection with jQuery's eq() . 您可以使用jQuery的eq()轻松缩小选择范围。

http://jsfiddle.net/BramVanroy/rzg64bvh/6/ http://jsfiddle.net/BramVanroy/rzg64bvh/6/

 $(document).ready(function () {
     $.ajax({
         url: 'http://tubecrush.net/wp_api/v1/posts?callback=show',
         dataType: "jsonp",
         type: "get",
         success: function (data) {
             var regex = /"/g;
             var i = 0;
             for (i = 0; i < 20; i++) {
                 var content = data.posts[i].content_display;
                 var part1 = content.split('src="');
                 var part2 = part1[1].split(regex);
                 var imgPath = part2[0];
                 console.log(imgPath);
                 $('.crush').eq(i).html('<img src="' + imgPath + '">');
             }
         }
     });
 });

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

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