-1

当我单击锚标记时,此代码会出现额外的段落,但我不希望所有这些函数因为它变得混乱我想知道如何将这段代码压缩成一个函数我不介意 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>';
                }
            }
4

3 回答 3

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>';
    }
}
于 2013-05-25T15:34:05.893 回答
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>';
            }
        }
于 2013-05-25T15:34:39.597 回答
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');
于 2013-05-25T15:35:13.977 回答