简体   繁体   English

随机获取n个元素数组,并将其插入指定的元素中

[英]Take a random n number array of elements and insert them in a specified element

I have a list of books that are embedded frame elements. 我有一个嵌入框架元素的书籍清单。 I'm wanting to return three of those books and insert them into a div I've already defined. 我想退回其中三本书并将它们插入到我已经定义的div I'm trying to utilize the shuffle method, but something isn't going right as I can't get the elements to display or randomize. 我正在尝试利用随机播放方法,但是由于无法获取要显示或随机化的元素,所以出现了一些错误。

The code below is the example I'm trying to work with. 以下代码是我尝试使用的示例。

Javascript Java脚本

   function randomBooks(arr, count) {

   var arr = [ // list of books
   '<iframe type="text/html" width="150" height="200" frameborder="0" allowfullscreen style="max-width:100%" src="https://read.amazon.com/kp/card?asin=B00ARFNQ54&asin=B00ARFNQ54&preview=newtab&linkCode=kpe&ref_=cm_sw_r_kb_dp_wpdGxbH9ZAXX9&hideShare=true" ></iframe>',
   '<iframe type="text/html" width="150" height="200" frameborder="0" allowfullscreen style="max-width:100%" src="https://read.amazon.com/kp/card?asin=B00AFH1TBC&asin=B00AFH1TBC&preview=newtab&linkCode=kpe&ref_=cm_sw_r_kb_dp_5udGxbAPXN07Q&hideShare=true" ></iframe>',
   '<iframe type="text/html" width="150" height="200" frameborder="0" allowfullscreen style="max-width:100%" src="https://read.amazon.com/kp/card?asin=B005GSYZRA&asin=B005GSYZRA&preview=inline&linkCode=kpe&ref_=cm_sw_r_kb_dp_FxdGxbYXKDM2P&hideShare=true" ></iframe>',
   '<iframe type="text/html" width="150" height="200" frameborder="0" allowfullscreen style="max-width:100%" src="https://read.amazon.com/kp/card?asin=B00ARFNQ54&asin=B00ARFNQ54&preview=newtab&linkCode=kpe&ref_=cm_sw_r_kb_dp_wpdGxbH9ZAXX9&hideShare=true" ></iframe>',
   '<iframe type="text/html" width="150" height="200" frameborder="0" allowfullscreen style="max-width:100%" src="https://read.amazon.com/kp/card?asin=B00AFH1TBC&asin=B00AFH1TBC&preview=newtab&linkCode=kpe&ref_=cm_sw_r_kb_dp_5udGxbAPXN07Q&hideShare=true" ></iframe>',
   '<iframe type="text/html" width="150" height="200" frameborder="0" allowfullscreen style="max-width:100%" src="https://read.amazon.com/kp/card?asin=B005GSYZRA&asin=B005GSYZRA&preview=inline&linkCode=kpe&ref_=cm_sw_r_kb_dp_FxdGxbYXKDM2P&hideShare=true" ></iframe>'

   ];    

   var insertDiv = document.getElementsByClassName("bookFrame"); // Div I want elements inserted

   var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
   while (i-- > min) {
       index = Math.floor((i + 1) * Math.random());
       temp = shuffled[index];
       shuffled[index] = shuffled[i];
       shuffled[i] = temp;
   }

   var returnedValue= shuffled.slice(min, 3);
   insertDiv.innerHTML(returnedValue); // inject 3 random book elements

   alert( randomBooks(arr, 3)); // Checking 

   };

HTML 的HTML

<p class="center bookFrame">

<!-- Insert books here -->

</p>

There are multiple errors in this code. 此代码中存在多个错误。

First, the alert( randomBooks(arr, 3)); // Checking 首先, alert( randomBooks(arr, 3)); // Checking alert( randomBooks(arr, 3)); // Checking appears to be inside the function randomBooks itself, so clearly it will never be called.. alert( randomBooks(arr, 3)); // Checking似乎是在randomBooks函数内部,所以很明显它永远不会被调用。

Secondly, you can't do insertDiv.innerHTML(returnedValue); 其次,您不能执行insertDiv.innerHTML(returnedValue); as 'innerHTML' is a string, not a function. 因为“ innerHTML”是字符串,而不是函数。 And the return value of getElementsByClassName is a list and not a single element, so it should have been something like: 而且getElementsByClassName的返回值是一个列表,而不是一个元素,因此应该是这样的:

insertDiv[0].innerHTML = returnedValue;

There might be more errors, it looks like this code never really ran, so I suggest you start executing it and see how it goes first. 可能还会出现更多错误,看起来这段代码从未真正运行过,所以我建议您开始执行它,然后看看它如何运行。

Just a tip: for testing purposes, instead of adding random calls and alerts to the code you could use the developer console (in chrome its F12, go to the console tab and just call randomBooks() from there to see what it returns). 提示:为了进行测试,您可以使用开发人员控制台(而不是在代码中添加随机调用和警报)(在F12中,转到控制台选项卡,然后从那里调用randomBooks()来查看返回的内容)。

Things might not have been working for a few reasons. 出于某些原因,事情可能没有起作用。 There were a bunch of little errors in the code. 代码中有很多小错误。

Your randomBooks() method accepts an argument arr, but you then overwrote it with your own. 您的randomBooks()方法接受参数arr,但是您随后用自己的参数覆盖了它。 I think you might have wanted to pass in the array of books into your randomizer so I did that in my example. 我认为您可能希望将书本数组传递到您的随机数发生器中,所以我在我的示例中做到了。

Secondly, getElementsByClassName() returns an array. 其次, getElementsByClassName()返回一个数组。 You might want to take the first element of it or switch to querySelector() if you know that you want the first of selector found. 如果您知道要找到第一个选择器,则可能要使用它的第一个元素或切换到querySelector()

Finally, el.innerHTML is directly assigned to. 最后,直接将el.innerHTML分配给它。 You have used it more like how jQuery's $el.html() method works. 您使用它的方式类似于jQuery的$ el.html()方法的工作方式。

 function addBooks(target, bookIds, count) { // ----------------------------- // A classic array shuffler (inplace). // ----------------------------- function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } // ----------------------------- // ----------------------------- // Get 3 random books. // ----------------------------- shuffleArray(bookIds); var selected = bookIds.slice(0, 3); // ----------------------------- // ----------------------------- // Construct some HTML for the 3 books. // ----------------------------- var html = selected.map(function(bookId){ var retval = '<iframe type="text/html" frameborder="0" allowfullscreen src="https://read.amazon.com/kp/card?asin=' + bookId + '&asin=' + bookId + '&preview=newtab&linkCode=kpe&ref_=cm_sw_r_kb_dp_wpdGxbH9ZAXX9&hideShare=true" ></iframe>'; return retval; }); // ----------------------------- // ----------------------------- // inject 3 random book elements // ----------------------------- document.querySelector(target).innerHTML = html; // ----------------------------- }; var bookIds = [ 'B00ARFNQ54', 'B00AFH1TBC', 'B005GSYZRA', 'B00ARFNQ54', 'B00AFH1TBC', 'B005GSYZRA' ]; addBooks(".bookFrame", bookIds, 3); 
 iframe { width: 150px; height: 200px; max-width:100%; } 
 <p class="center bookFrame"><!-- Insert books here --></p> 

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

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