当我点击一个锚标签时,这段代码会显示额外的段落,但我不希望所有这些功能因为它变得混乱我想知道如何将这段代码压缩成一个单独的函数我不介意jquery但我会喜欢javascript,但只是做更有效率的事情。
function showmore1()
{
var more_1 = document.getElementById("learnmore_1");
if (more_1.style.display == "block")
{
more_1.style.display = "none";
document.getElementById('showmore_1').innerHTML ='<a>Even More History</a>';
}
else
{
more_1.style.display = "block";
document.getElementById('showmore_1').innerHTML ='<a>Show Less</a>';
}
}
function showmore2()
{
var more_2 = document.getElementById("learnmore_2");
if (more_2.style.display == "block")
{
more_2.style.display = "none";
document.getElementById('showmore_2').innerHTML ='<a>Even More Why programming</a>';
}
else
{
more_2.style.display = "block";
document.getElementById('showmore_2').innerHTML ='<a>Show Less</a>';
}
}
function showmore3()
{
var more_3 = document.getElementById("learnmore_3");
if (more_3.style.display == "block")
{
more_3.style.display = "none";
document.getElementById('showmore_3').innerHTML ='<a>Even More Skills</a>';
}
else
{
more_3.style.display = "block";
document.getElementById('showmore_3').innerHTML ='<a>Show Less</a>';
}
}
function showmore4()
{
var more_4 = document.getElementById("learnmore_4");
if (more_4.style.display == "block")
{
more_4.style.display = "none";
document.getElementById('showmore_4').innerHTML ='<a>Even More Accomplishments</a>';
}
else
{
more_4.style.display = "block";
document.getElementById('showmore_4').innerHTML ='<a>Show Less</a>';
}
}
答案 0 :(得分:0)
也许这项工作我不测试它
function showmore(number,moreMessage,lessMessage)
{
var more = document.getElementById("learnmore_"+number);
if (more.style.display == "block")
{
more.style.display = "none";
document.getElementById('showmore_'+number).innerHTML ='<a>'+moreMessage+'</a>';
}
else
{
more.style.display = "block";
document.getElementById('showmore_'+number).innerHTML ='<a>'+lessMessage+'</a>';
}
}
答案 1 :(得分:0)
function showmore(id, moreMessage, lessMessage)
{
var more = document.getElementById('more'+id);
if (more.style.display == "block")
{
more.style.display = "none";
document.getElementById('showmore_'+id).innerHTML ='<a>'+moreMessage+'</a>';
}
else
{
more.style.display = "block";
document.getElementById('showmore_'+id,).innerHTML ='<a>'+lessMessage+'</a>';
}
}
答案 2 :(得分:0)
拉出函数内部更改(变量)的代码位,然后组合功能并将变量传递给要插入的函数。
function showmore(id, more, less) {
var more = document.getElementById('learnmore_'+id);
if(more.style.display == "block") {
more.style.display = "none";
document.getElementById('showmore_'+id).innerHTML ='<a>'+more+'</a>';
} else {
more.style.display = "block";
document.getElementById('showmore_'+id).innerHTML ='<a>'+less+'</a>';
}
}
然后像这样使用它:
showmore(1, 'Even more accomplishments', 'Show less');