jquery - 使用 Jquery 显示三个 Div 中的一个随机 Div

标签 jquery html random

我有一个包含图像和文本的三个 div (div="frames") 的列表。我需要在每次加载页面时随机显示其中之一。这是我开始的代码:

<script>
var random = Math.floor(Math.random() * $('.frames').length);
Math.floor(Math.random() * $('.frames').length);
$('.frames').hide().eq(random).show();
</script>

<div class="frames">
<div class="frame1">
<h1>HEADER</h1>
<p>
PARAGRAPH
</p>
<a href="index.php?page=everstock-program"><img class="button1" src="images/learn-more-about-everstock.png" alt="Learn more about Everstock&trade;" /></a>
<p class="testimonial">
TESTIMONIAL
</p>
</div><!--frame1-->
</div>
<div class="frames">
<div class="frame2">
<h1>HEADER</h1>
<p>
PARAGRAPH
</p>
<a href="index.php?page=electrical-safety-training"><img class="button2" src="images/learn-more-about-safety.png" alt="Learn more about safety" /></a>
<p class="testimonial">
TESTIMONIAL
</p>
</div><!--frame2-->
</div>
<div class="frames">
<div class="frame3">
<h1>HEADER</h1>
<p>
PARAGRAPH
</p>
<a href="index.php?page=accu-inventory"><img class="button3" src="images/learn-more-about-accu-inventory.png" alt="Learn more about Accu-Inventory&trade;" /></a>
<p class="testimonial">
TESTIMONIAL
</p>
</div><!--frame3-->
</div><!--frames-->

所有这些都会以 display:none 开始,我想随机选取其中一个 div 并将它们设置为 display:block。任何指示将不胜感激。我不太擅长 jquery 编码。

CSS=

.frames{
position: relative;
top: 0;
left: 11px;
margin: 0;
padding: 0;
width: 908px;
height: 373px;
z-index: 10;
}

.frames .frame1{
background: transparent url("http://royalelectric.com/blog/wp-content/uploads/2014/07/frame11.jpg") no-repeat top left;
width: 906px;
height: 373px;
margin: 0;
padding: 0;
}

.frames .frame1 h1{
color: #151b65;
margin: 35px 0  5px 25px;
font-weight: normal;
font-size: 30px;
width: 350px;
line-height: 35px;
}

.frames .frame1 p{
width: 500px;
margin-left: 25px;
font-size: 14px;
color: #151b65;
}

.frames .frame1 .button1{
margin: 0 0 0 20px;
}

.frames .frame1 .testimonial{
color: #151b65;
font-style: italic;
width: 400px;
margin: 0 0 0 25px;
}

.frames .frame2{
background: transparent url("http://royalelectric.com/blog/wp-content/uploads/2014/07/frame21.jpg") no-repeat top left;
width: 906px;
height: 373px;
margin: 0;
padding: 0;
}

.frames .frame2 h1{
color: #151b65;
margin: 15px 0  5px 425px;
font-weight: normal;
font-size: 30px;
width: 450px;
line-height: 35px;
text-align: right;
}

.frames .frame2 p{
width: 500px;
margin: 5px 0 5px 375px;
font-size: 14px;
text-align: right;
color: #151b65;
}

.frames .frame2 .button2{
margin: 0 0 0 665px;
}

.frames .frame2 .testimonial{
color: #151b65;
font-style: italic;
width: 400px;
margin: 0 0 0 475px;
text-align: right;
}

.frames .frame3{
background: transparent url("http://royalelectric.com/blog/wp-content/uploads/2014/07/frame31.jpg") no-repeat top left;
width: 906px;
height: 373px;
margin: 0;
padding: 0;
}

.frames .frame3 h1{
color: #151b65;
margin: 15px 0  5px 25px;
font-weight: normal;
font-size: 30px;
width: 450px;
line-height: 35px;
}

.frames .frame3 p{
width: 525px;
margin-left: 25px;
font-size: 14px;
color: #151b65;
}

.frames .frame3 .button3{
margin: 0 0 0 20px;
}

.frames .frame3 .testimonial{
color: #151b65;
font-style: italic;
width: 400px;
margin: 0 0 0 25px;
}

#frame_nav{
position: relative;
margin: -35px 0 0 408px;
z-index: 500;
height: 30px;
width: 90px;
}

#frame_nav ul{
list-style: none;
margin: 0;
padding: 0;
}

#frame_nav li{
margin: 0;
padding: 0;
float: left;
}

谢谢。

最佳答案

您需要将代码放在页面末尾或将其包装在 document ready call 中。事实上,您正在尝试对尚不存在的元素执行代码。

关于jquery - 使用 Jquery 显示三个 Div 中的一个随机 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25207578/

相关文章:

JavaScript - 5 秒后单击所有带有类名的按钮 (Twitter)

javascript - 如何删除除一个以外的所有切换?

javascript - 创建上一个/下一个 html 页面导航

javascript - 在类型上显示单选值

html - 元素出国有自己的背景

r - 在 R 中,从 df 中采样 n 行,其中某个列具有非 NA 值(有条件地采样)

javascript - 如何引用复选框数组名称值?

javascript - 是否可以在 title 属性中放置链接?

java - 生成唯一的随机数

C++11:如何使用 <random> 设置种子