简体   繁体   English

JavaScript / jQuery-如何在页面加载时随机显示html div内容

[英]JavaScript/jQuery - how to show html div content randomly on page load

This is my first ever question here so please bear with me (also new to programming) I will try my best to explain as clearly as I can. 这是我在这里的第一个问题,所以请耐心听(也是编程新手),我将尽我所能尽力解释。 Basically I want to show the div content in random order everytime the page is loaded, this question has already been asked before and I have come across some answers to this but most of them are quite complicated for me. 基本上,我希望每次加载页面时以随机顺序显示div内容,之前已经有人问过这个问题,并且我已经找到了一些答案,但是其中大多数对我来说都很复杂。 But I found a very simple answer ( Random Div Order on Page Load ) that I tried but I am not sure why won't it work for me, may be it's a little outdated?. 但是我找到了一个非常简单的答案( 页面加载上的随机Div顺序 ),但是我不确定为什么它对我不起作用,可能有点过时了。 I am pasting the code below to visualise for you lot, here are the divs in the html content that need to be randomised: 我正在粘贴下面的代码以使您形象化,这是需要随机化的html内容中的div:

 <div class="story-container">
  <div class="story">
    <a href="#"><img src="#" alt="some text"></a>
      <h4>Story Title</h4>
  </div>
  <div class="story">
    <a href="#"><img src="#" alt="some text"></a>
      <h4>Story Title</h4>
  </div>
</div>
<div class="story-container">
 <div class="story">
  <a href="#"><img src="#" alt="some text"></a>
    <h4>Story Title</h4>
 </div>
 <div class="story">
  <a href="#"><img src="#" alt="some text"></a>
    <h4>Story Title</h4>
</div>

I may add more 'story-container' class divs in the future. 将来我可能会添加更多的“ story-container”类div。

Below is the script that I got from the post given in the link I provided above (I replaced the target class with my own class 'story'): 以下是我从上面提供的链接中给出的帖子中获得的脚本(我用自己的类“ story”替换了目标类):

<script>
 var cards = $(".story");
 for (var i = 0; i < cards.length; i++) {
 var target1 = math.floor(math.random() * cards.lenth - 1) + 1;
 var target2 = math.floor(math.random() * cards.lenth - 1) + 1;
 cards.eq(target1).before(cards.eq(target2));
}
</script>

I have tried using this code javascript code within the same html page, in the header as well as at the end of body part. 我已经尝试在同一HTML页面,标头以及正文部分的末尾使用此代码javascript代码。 I also tried saving it externally and linked to it from within my html page but no luck. 我也尝试将其保存在外部并从我的html页面中链接到它,但是没有运气。

My jQuery link/version is as follows: 我的jQuery链接/版本如下:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3
/jquery.min.js"></script>

I import this jQuery code before running the js script to randomise the divs. 我先导入此jQuery代码,然后再运行js脚本以使div随机化。 Not sure where things are going wrong, any help will be greatly appreciated, thank very much in advance guys! 不知道哪里出了问题,将非常感谢您的帮助,在此先感谢大家!

[PROGRESS] [进展]

Thank you very much guys for your prompt replies and detailed guidance, @Hill @fermats_last_nerve @jritchey @Quiver, although you fixed the code but still it is not working in my browser which is really strange because it works perfectly in jsfiddle, codepen and plunkr. 非常感谢您的及时答复和详细的指导,@ Hill @fermats_last_nerve @jritchey @Quiver,尽管您已修复了代码,但仍然无法在我的浏览器中使用,这确实很奇怪,因为它在jsfiddle,codepen和plunkr中完美地工作。 If it is working in online tools it should work in my browser as well. 如果它在在线工具中工作,它也应该在我的浏览器中工作。

I am using WAMP as a server to test my php pages on Windows 10, I normally use Firefox browser but I have test this code on Microsoft Edge as well but no joy. 我使用WAMP作为服务器在Windows 10上测试我的php页面,我通常使用Firefox浏览器,但我也在Microsoft Edge上测试了此代码,但没有任何乐趣。 Thank you again for taking time to help me with this. 再次感谢您抽出宝贵时间为我提供帮助。

I debugged your code in this codepen . 我在此Codepen中调试了您的代码。 Basically you had 3 errors 基本上你有3个错误

  1. There are typos in your definitions for target1 and target2 cards.lenth should be cards.length target1和target2 cards.lenth定义中有错别字cards.lenth应该是cards.length
  2. The variable cards: var cards = $(".article") is selecting all elements with the class "article", but you don't have any elements with that class, I assume you mean var cards = $(".story") 变量卡: var cards = $(".article")正在选择类别为“ article”的所有元素,但该类别中没有任何元素,我假设您的意思是var cards = $(".story")
  3. Make sure you capitalize Math when you use it. 确保在使用Math时将其大写。

    (EDIT) Looks like someone edited your post to have the correct class in the selector so #2 no longer makes sense in light of the new edited question but I'll leave it in case you do not notice the edit for whatever reason. (编辑)好像有人编辑了您的帖子以使选择器中的类正确,因此鉴于新编辑的问题,#2不再有意义,但是如果您出于某种原因而没有注意到该编辑,我将保留它。

I've noticed a few typos in your code. 我注意到您的代码中有一些错字。 Replace your jQuery with the code below and it should work as it does in this jsfiddle . 用下面的代码替换您的jQuery,它应该像在jsfiddle中一样工作

var cards = $(".story");
for (var i = 0; i < cards.length; i++) {
  var target1 = Math.floor(Math.random() * cards.length - 1) + 1;
  var target2 = Math.floor(Math.random() * cards.length - 1) + 1;
  cards.eq(target1).before(cards.eq(target2));
}

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

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