繁体   English   中英

使用jquery在给定类中创建元素数组,然后在该类中选择一个随机链接并打开它

[英]using jquery to create array of elements in a given class then selecting a random link within that class and opening it

var linksInCategory = document.id($('.CategoryTreeLabel').href);
var randomLinkArray = new Array(linksInCategory);

// CategoryTreeLabel是所有锚标签都包含的类,该类包含带有指向所需页面链接的href

     function goThere(link)
{
        var the_url = function pickRandomURL () {
            var random_url = randomLinkArray[Math.floor(Math.random()*randomLinkArray.length)];
            the_url = random_url;

        }
        var good_url = fixURL(the_url);
        var new_window = window.open(good_url,"new_window","menubar, resizeable. location, toolbar, status, scrollbars");
}


function fixURL(the_url) {
        var the_first_seven = the_url.substring(0,7);
        the_first_seven = the_first_seven.toLowerCase();
        if (the_first_seven != 'http://') 
        {
                the_url = "http://" + the_url;
        }
        return the_url;
}
</script>
</head>
<body>
<form name="the_form">

<input type="button" name="the_url" class="broadGroups" onClick="goThere(this)" src="the_url" value="Sports"></input>

<input type="button" name="the_url" class="broadGroups" onClick="goThere(this)"  src="the_url" value="Film"></input>

基本上,我想在与class =“ CategoryTreeLabel”相同的标记内创建所有href链接的数组,然后我想创建一个函数goThere(),该函数将打开一个带有good_url URL的新窗口。 需要从我们从标签中获取的链接列表中随机选择the_url,该链接在文档中具有“ CategoryTreeLabel”类。

每个按钮都应该调用goThere(this)函数,并从我们创建的数组中选择一个随机URL,检查它是否具有http://(它总是会重定向到没有它的页面,但我还是把它留在了里面很有趣),然后打开该页面

jQuery函数的返回值是一个类似数组的对象,也就是说,它具有.length属性,可以使用array-style []括号表示法进行访问,因此您实际上不需要创建单独的数组变量太。

我注意到您的按钮似乎是针对不同类别的链接,例如体育或电影,因此您的意图可能是“体育”按钮将选择与体育相关的随机链接,而“电影”按钮将选择与电影相关的随机链接,相关链接? 如果是这样,您可以让每个按钮将类别传递到goThere()函数,然后从该类别中选择一个随机链接。 像这样:

function goThere(category)
{
    // assume that the parameter is the class name for links
    // in the desired category
    var $myLinks = $("a." + category);

    // check if there are any matching links
    if ($myLinks.length === 0) {
        alert("Sorry, no links in the " + category + " category.");
        return;
    }

    var url = fixURL($myLinks[ Math.floor(Math.random()*$myLinks.length) ].href);

    var new_window = window.open(url,"new_window",
          "menubar, resizeable. location, toolbar, status, scrollbars");
}

然后,您将设置锚标记,使其类名称具有适当的类别,如下所示:

<a class="sports" href="http://somesportssite.com">Super sports</a>
<a class="film"   href="http://moviesRus.com">Movies</a>
<a class="film"   href="http://animationplus.com">All about animation</a>
<a class="sports" href="http://football.com">Football site</a>
<a class="sports" href="http://skydiving.com">Let's jump!</a>

和相关的按钮将是:

<input type="button" value="Sports">
<input type="button" value="Film">

您可以像设置onclick="sports"一样设置内联处理程序,也可以在document.ready处理程序中执行类似以下操作,以使用单个jQuery .click()调用.click()假设相应的类名/ category是按钮标签的小写版本:

$('input[type="button"]').click(function() {
    goThere(this.value.toLowerCase());
});

暂无
暂无

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

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