html:
<p><span>JavaScript 是属于网络的脚本语言!...</span><a href="javascript:;">>>收缩</a></p>
css:
a {text-decoration: none;}/* 去掉a的默认下划线*/
js:
var oP = document.getElementsByTagName('p')[0];
var oSpan = document.getElementsByTagName('span')[0];
var oA = document.getElementsByTagName('a')[0];
var str = oSpan.innerHTML;//获取文字内容,方便隐藏和显示
var onOff = true;//开关
oA.onclick = function(){
if(onOff){
oSpan.innerHTML = str.substring(0,18);
oA.innerHTML = '......>>展开';
}else{
oSpan.innerHTML = str;
oA.innerHTML = '<<收缩';
}
onOff = !onOff;//每次点击之后都取反
};
注意:
每次点击完之后一定要取反onOff = !onOff
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!