Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将多个参数传递到单个参数中_Javascript_Html - Fatal编程技术网

Javascript 将多个参数传递到单个参数中

Javascript 将多个参数传递到单个参数中,javascript,html,Javascript,Html,我在internet上找到一个脚本,它显示并隐藏代码片段: function show(shown, hidden,) { document.getElementById(shown).style.display='block'; document.getElementById(hidden).style.display='none'; return false; } <a href="#" onclick="return show('Page1','Pag

我在internet上找到一个脚本,它显示并隐藏代码片段:

 function show(shown, hidden,) {
    document.getElementById(shown).style.display='block';
    document.getElementById(hidden).style.display='none';
    return false;
 }


<a href="#" onclick="return show('Page1','Page2');">Page 1</a>
<a href="#" onclick="return show('Page2','Page1');">Page 2</a>
函数显示(显示、隐藏){
document.getElementById(如图所示).style.display='block';
document.getElementById(隐藏).style.display='none';
返回false;
}
我的问题是,我将如何修改它以包含更多页面

我能找到的唯一有效方法是:

    function show(shown, hidden1, hidden2, hidden3) {
      document.getElementById(shown).style.display='block';
      document.getElementById(hidden1).style.display='none';
      document.getElementById(hidden2).style.display='none';
      document.getElementById(hidden3).style.display='none';
      return false;
    }


<a href="" onclick="return show('Page1','Page2','Page3','Page4' );">Page 1</a>
<a href="" onclick="return show('Page2','Page1','Page3','Page4' );">Page 2</a>
<a href="" onclick="return show('Page3','Page1','Page2','Page4' );">Page 3</a>
<a href="" onclick="return show('Page4','Page1','Page2','Page3' );">Page 4</a>
功能显示(显示为hidden1、hidden2、hidden3){
document.getElementById(如图所示).style.display='block';
document.getElementById(hidden1.style.display='none';
document.getElementById(hidden2.style.display='none';
document.getElementById(hidden3.style.display='none';
返回false;
}
…但看起来有点乱。那么,有没有一种方法可以一次将3页全部传递到脚本中的“隐藏”参数中


谢谢

可以将数组作为
隐藏的
参数传递。然后您必须修改JS,如下所示:

function show(shown, hidden ) {
    document.getElementById(shown).style.display='block';
    for( var i=hidden.length; i--; ) {
      document.getElementById(hidden[i]).style.display='none';
    }
    return false;
 }
<a href="" onclick="return show('Page1', ['Page2','Page3','Page4'] );">Page 1</a>
HTML将如下所示:

function show(shown, hidden ) {
    document.getElementById(shown).style.display='block';
    for( var i=hidden.length; i--; ) {
      document.getElementById(hidden[i]).style.display='none';
    }
    return false;
 }
<a href="" onclick="return show('Page1', ['Page2','Page3','Page4'] );">Page 1</a>

您可以将数组作为第二个参数传递,甚至可以使用已经存在的
参数
类数组对象:

function show() {
    document.getElementById(arguments[0]).style.display='block';
    for(var i=1; i<arguments.length; i++){
        document.getElementById(arguments[i]).style.display='none';
    }
}
函数显示(){
document.getElementById(参数[0]).style.display='block';
对于(var i=1;i,您可以使用、传递对象建议的数组或使用选择所有节点(例如类)的方法,然后仅显示所需的节点

// arguments
function show() {
    var i = arguments.length;
    if (i > 0) {
        while (--i) document.getElementById(arguments[i]).style.display = 'none';
        document.getElementById(arguments[0]).style.display = 'block';
    }
}
// show('vis_id_1', 'hid_id_1', 'hid_id_2',...)

// object
function show(obj) {
    var id;
    for (id in obj) {
        if (obj.hasOwnProperty(id)) { // safety filter
            document.getElementById(id).style.display = obj[id];
        }
    }
}
// show({'vis_id_1':'block', 'hid_id_1':'none'});

非常感谢,它工作完美,正是我所期待的:)