Javascript 如何在事件上显示和切换不同的div id
该代码将只显示列表2。。。我不知道为什么Javascript 如何在事件上显示和切换不同的div id,javascript,html,Javascript,Html,该代码将只显示列表2。。。我不知道为什么 是的,我对Javascript非常陌生,但我在PHP之前就接触过,但它看起来不像Javascript。这应该适合您: 删除id参数: function display(action, id) { document.getElementById("List"+id).style.display = "block"; document.getElementById("link"+id).innerHTML = "MoreVideo";
是的,我对Javascript非常陌生,但我在PHP之前就接触过,但它看起来不像Javascript。这应该适合您: 删除id参数:
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";
}
}
这将在
1
和2
之间切换id
只是想确认一下,如果语句是在1和2之间交换语句,而不是每次单击时在id中添加1,那么您体内的CERBUR是什么。我用您的代码替换我的代码,但结果是相同的(它只显示列表2)将“id=(id===1)?2:1使代码只显示列表2?@OnedZair:您要传递给函数的id
是什么?@Cerbrus id设置为1(),对吗?
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";
}
}
<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.
}
}
if(id === 1){
id = 2;
}else{
id = 1;
}