js函数编写及调用

谁帮忙把下面一段代码用一个函数调用写出来,在网页是要用到doclick1()-doclick10()下面这样的函数要写10几个,我想用switch case
再写一个函数, 这样就简单了
function doClick1(o){
o.className="taba";
var z;
var id;
var a;
for(var i=1;i<=2;i++){
id ="aa"+i;
z = document.getElementById(id);
a = document.getElementById("a"+i);
if(id != o.id){
z.className="tabb";
a.style.visibility = "hidden";
}else{
a.style.visibility = "visible";
}
}
}
function doClick2(o){
o.className="taba";
var z;
var id;
var b;
for(var i=1;i<=2;i++){
id ="bb"+i;
z = document.getElementById(id);
b = document.getElementById("b"+i);
if(id != o.id){
z.className="tabb";
b.style.visibility = "hidden";
}else{
b.style.visibility = "visible";
}
}
}
function doClick3(o){
o.className="taba";
var z;
var id;
var c;
for(var i=1;i<=2;i++){
id ="cc"+i;
z= document.getElementById(id);
c= document.getElementById("c"+i);
if(id != o.id){
z.className="tabb";
c.style.visibility = "hidden";
}else{
c.style.visibility = "visible";
}
}
}

function doClick4(o){
o.className="taba";
var z;
var id;
var d;
for(var i=1;i<=2;i++){
id ="dd"+i;
z = document.getElementById(id);
d = document.getElementById("d"+i);
if(id != o.id){
z.className="tabb";
d.style.visibility = "hidden";
}else{
d.style.visibility = "visible";
}
}
}

function doClick5(o){
o.className="taba";
var z;
var id;
var e;
for(var i=1;i<=2;i++){
id ="ee"+i;
z = document.getElementById(id);
e = document.getElementById("e"+i);
if(id != o.id){
z.className="tabb";
e.style.visibility = "hidden";
}else{
e.style.visibility = "visible";
}
}
}

function doClick6(o){
o.className="taba";
var z;
var id;
var f;
for(var i=1;i<=2;i++){
id ="ff"+i;
z = document.getElementById(id);
f = document.getElementById("f"+i);
if(id != o.id){
z.className="tabb";
f.style.visibility = "hidden";
}else{
f.style.visibility = "visible";
}
}
}

function doClick7(o){
o.className="taba";
var z;
var id;

for(var i=1;i<=2;i++){
id ="gg"+i;
z = document.getElementById(id);
g = document.getElementById("g"+i);
if(id != o.id){
z.className="tabb";
g.style.visibility = "hidden";
}else{
g.style.visibility = "visible";
}
}
}

function doClick8(o){
o.className="taba";
var z;
var id;

for(var i=1;i<=2;i++){
id ="hh"+i;
z = document.getElementById(id);
h = document.getElementById("h"+i);
if(id != o.id){
z.className="tabb";
h.style.visibility = "hidden";
}else{
h.style.visibility = "visible";
}
}
}

1.函数直接写在页面相当于是本页调用,所以理所应当可以调用

2.js单独文件不能调用是因为你没有引用js文件,如果引用了的话,也是可以调用的。
引用方式,可以直接拖拽(我一般都是拖拽,因为路径准确),也可以自己写,引用脚本:<script src="Scripts/JScript.js" type="text/javascript"></script>
3.还有就是,引用的js文件一定要在调用之前,否则就找不到函数了,会报错或undefined
例如:
调用页面:
<script src="Scripts/JScript.js" type="text/javascript"></script>
<script type="text/javascript">
aa();
</script>
Script.js中代码:

function aa() {
alert("js文件掉用");
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-08-11
function doClick(o, n){
o.className="taba";
var z;
var id;

var ids = new Array();
ids[1] = 'aa';
ids[2] = 'bb';
ids[3] = 'cc';
ids[4] = 'dd';
ids[5] = 'ee';
ids[6] = 'ff';
ids[7] = 'gg';
ids[8] = 'hh';
ids[9] = 'ii';
ids[10] = 'jj';

for(var i=1;i<=2;i++){
id =ids[n]+i;
z = document.getElementById(id);
h = document.getElementById(ids[n].substr(0,1)+i);
if(id != o.id){
z.className="tabb";
h.style.visibility = "hidden";
}else{
h.style.visibility = "visible";
}
}
}

你试试这样可以吗, 调用方式改为
doClick(obj, n), n=1-10
第2个回答  推荐于2017-11-23
function doClick(o,p)
{
o.className="taba";
for(var i=1;i<=2;i++){
var id = p+p+i;
var z = document.getElementById(id);
var h = document.getElementById(p+i);
if(id != o.id){
z.className="tabb";
h.style.visibility = "hidden";
}
else
{
h.style.visibility = "visible";
}
}
}

调用:doClick(this,'a');doClick(this,'h');本回答被提问者采纳
第3个回答  2015-11-07
function doClick(o,p)
o.className="taba";
for(var i=1;i<=2;i++)
var id = p+p+i;var z = document.getElementById(id);
var h = document
.getElementById(p+i);
if(id != o.id){
z.className="tabb";
h.style.visibility = "hidden";
else
h.style.visibility = "visible";

调用:doClick(this,'a');doClick(this,'h')。
希望这些答案对你有帮助!
相似回答