JS:检查URL中的参数,然后使用参数var作为迭代函数

时间:2015-04-10 01:13:30

标签: javascript html


我有以下代码:

<div id="1" class="hide"> Text1 </div>
<div id="2" class="hide"> Text2 </div>
<div id="3" class="hide"> Text3 </div>
<div id="4" class="hide"> Text4 </div>
<div id="5" class="hide"> Text5 </div>

我希望所有这些div都被一个能够检查的函数隐藏:
- 如果URL包含的参数等于div之一的ID - 对于每个div,这样做:检查URL是否包含1到5的参数 - 隐藏除ID与参数匹配的元素之外的所有元素。网址中只有一个参数同时存在。

这是功能代码,它是独立的,因此该功能可以被其他链接调用:

function showOne(id) {
$('.hide').not('#' + id).hide(); }


这是迭代循环,现在我不知道如何以这种特定的方式将它们拼凑在一起我需要任何指示都会非常感激。

 while (i<6) {  
  if (window.location.search.indexOf('i=yes') > -1)
    {
    showOne(i)
    }} 

1 个答案:

答案 0 :(得分:2)

您没有迭代i并且您正在检查字符串"i=yes"而不是"1=yes"

while (i<6) {  
    if (window.location.search.indexOf(i + '=yes') > -1)
    {
        showOne(i);
        break;
    }
    i++;
}