0
<a id="link1" href="javascript:display('show', 1)">Show</a>

<div id="list1" style="display:none">
VIDEO 1</br>
VIDEO 2</br>
VIDEO 3</br>
</div>
<div id="list2" style="display:none"> 
VIDEO 4</br>
VIDEO 5</br>
VIDEO 6</br>
</div>

当我单击“显示”时,我想显示列表 1。

当我再次单击“显示”时,它必须显示列表 2,而列表 1 将消失/隐藏。

这是我要用于它的 Javascript 代码:

function display(action, id)
{
    document.getElementById("List"+id).style.display = "block";
    document.getElementById("link"+id).innerHTML = "MoreVideo";
    action = "freezes";
    document.getElementById("link"+id).onclick=action = "show";

    if(action == "show")
    {
        document.getElementById("List"+id).style.display = "none";
        id = id+ 1;
        document.getElementById("List"+id).style.display = "block";
    }
}

该代码只会显示列表 2...我不知道为什么。

是的,我对 Javascript 很陌生,但我在 PHP 之前做过,但它看起来不像 Javascript。

4

1 回答 1

2

这应该适合你:

去掉 id 参数:

<a id="link1" href="javascript:display('show')">Show</a>
var id = 2;
function display(action) {
    var div = document.getElementById("List"+id); // Don't 'get' the div that often, save it to a temporary variable.
    div.style.display = "block";
    div.innerHTML = "MoreVideo";
    // action = "freezes"; <-- You're setting `action` to "freezes", preventing the if below from executing
    // div.onclick = action = "show"; <-- That's not valid js

    if(action == "show") {
        div.style.display = "none"; // Hide the current div.
        id = (id === 1)? 2 : 1;      // Set the ID to that of the other div.
        document.getElementById("List"+id).style.display = "block"; // Show the other div.
    }
}

那么,这是什么id = (id === 1)? 2 : 1;

它是以下的简写:

if(id === 1){
    id = 2;
}else{
    id = 1;
}

这在和id之间切换;12

于 2013-01-03T13:08:27.537 回答