热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在刷新时显示页面的随机部分?-Howtodisplayrandompartsofapageonrefresh?

IfIweretohaveablockofhtmlcodingthatisstylizedviacsssuchasthis:如果我有一个html编码块,通过css风

If I were to have a block of html coding that is stylized via css such as this:

如果我有一个html编码块,通过css风格化,如下所示:


Test Paragraph 1

test link 1

Test Paragraph 2

test link 2

Test Paragraph 3

test link 3

I want to be able to display a set of parts randomly together, (i.e. title 1, paragraph 1 and link 1 only). Then if I refresh it randomly shows 1, 2 or 3. But they need to stay together, like parts of 1 cannot show with part of 3 and so on.

我希望能够随机地一起显示一组部分(即标题1,第1段和链接1)。然后,如果我刷新它随机显示1,2或3.但他们需要保持在一起,像1的部分不能显示3的一部分,依此类推。

I know this is simple, but I can't figure out how to get them to show randomly together. Thanks in advance!

我知道这很简单,但我无法弄清楚如何让它们随机显示。提前致谢!

4 个解决方案

#1


1  

The first step is to group all related parts together. See the HTML below. Note that title tags were replaced with heading tags. The title should be in the header of the page.

第一步是将所有相关部分组合在一起。请参阅下面的HTML。请注意,标题标签已替换为标题标签。标题应该在页面的标题中。

Test Title 1

Test Paragraph 1

test link 1

Test Title 2

Test Paragraph 2

test link 2

Test Title 3

Test Paragraph 3

test link 3

Then, in CSS you need to hide them:

然后,在CSS中你需要隐藏它们:

section {
    display: none;
}

And, here's jQuery stuff:

而且,这是jQuery的东西:

$("section").eq(Math.floor(Math.random() * 3)).css("display", "block");

Here's jsfiddle: http://jsfiddle.net/Aej52/

这是jsfiddle:http://jsfiddle.net/Aej52/

#2


0  

use jquery appendTo method

使用jquery appendTo方法

html code:

put this Javascript in onload event or document ready

把这个Javascript放在onload事件或文件准备好了

test link set yourself

测试链接设置自己

function showTitle(){
   var rand = Math.floor((Math.random() * 3) + 1);
   $("
      

Test Paragraph "+rand+"

test link "+rand+"" ).appendTo("#titleBox"); }

#3


0  

Here is a plain Javascript version - no jquery, with working demo

这是一个简单的Javascript版本 - 没有jquery,有工作演示

HTML



Test Paragraph 1

test link 1

Test Paragraph 2

test link 2

Test Paragraph 3

test link 3

Javascript

// get references to elements in question, and store them...
var titles = document.getElementsByTagName("title");
var ps = document.getElementsByTagName("p");
var as = document.getElementsByTagName("a");

// every now and then, run a function...
setInterval(function(){
    // set all elements to display none
    for(i=0;i

#4


0  

One option is to generate the elements via Javascript:

一种选择是通过Javascript生成元素:

http://jsfiddle.net/qmM36/

JS:

$(function () {
    var data = [
        {
            h1: 'Test Title 1',
            p: 'Test Paragraph 1',
            a: { href: '#test-link1', text: 'Test Link 1' }
        },
        {
            h1: 'Test Title 2',
            p: 'Test Paragraph 2',
            a: { href: '#test-link2', text: 'Test Link 2' }
        },
        {
            h1: 'Test Title 3',
            p: 'Test Paragraph 3',
            a: { href: '#test-link3', text: 'Test Link 3' }
        }
    ];
    //-- select an item at random
    var index = Math.floor( Math.random() * data.length );
    var item  = data[index];
    //-- create the HTML elements
    var $placeholder = $('#placeholder');
    $('

').text(item.h1).insertAfter($placeholder); $('

').text(item.p).insertAfter($placeholder); $('').attr('href', item.a.href).text(item.a.text).insertAfter($placeholder); });

HTML:

In this example, I used a placeholder div to mark where the items would be inserted.

在这个例子中,我使用占位符div来标记项目的插入位置。


推荐阅读
author-avatar
梦幻小螳螂
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有