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の間1で切り替わり2ます。

于 2013-01-03T13:08:27.537 に答える