随机选择一个超链接

时间:2012-12-07 15:29:10

标签: jquery hyperlink href

在我的博客上,我有12种不同的类型(音乐,摄影,建筑等......),然后在一个页面上至少有2种不同的每种类型的图像。我目前正在使用smoothscroll.js。当你点击摄影(例如)时,它将直接进入第一个超链接(X)

<a href="#photgraphy"> photography </a>




<a name="photography"> </a> [X]
<a name="photography"> </a> [Y]

有没有办法当你点击摄影时它选择随机 X或Y而不是它转到第一个超链接(X)。

1 个答案:

答案 0 :(得分:0)

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        $(document).ready(function(){
            // Create an array of links
            myLinks = new Array;
            myLinks[0] = "#one";
            myLinks[1] = "#two";
            myLinks[2] = "#three";

            // Slide to random link
            $("a.myRandomLink").click(function(){
                randomLink = Math.round(Math.random() * (myLinks.length-1));
                $("html, body").animate({scrollTop: $(myLinks[randomLink]).offset().top + "px"},{duration: 500, easing: "swing"});
                return false;
            });
        });
    </script>
</head>
<body>

    <a href="#" class="myRandomLink">Random link</a><br><br>

    <div id="one" style="width:100%;height:600px;background-color:#550000;"></div>
    <div id="two" style="width:100%;height:600px;background-color:#005500;"></div>
    <div id="three" style="width:100%;height:600px;background-color:#000055;"></div>
    <div id="four" style="width:100%;height:600px;background-color:#ffffff;"></div>

</body>
</html>