我在JavaScript入门经典(第五版),第13章 根据title选择样式表的例子如下:<!DOCTYPE html><html><head><title>Switch stylesheets with JavaScript</title><style title="sheet1">body{background-color:white;color:red;}</style><style title="sheet2">body{background-color:black;color:yellow;}</style><style title="sheet3">body{background-color:pink;color:green;}</style><script>function ssEnable(mySheet){var change=false;var oldSheet=0;for(var i=0;i<document.styleSheets.length;i++){if(document.styleSheets[i].disabled==false){oldSheet=i;}document.styleSheets[i].disabled=true;if(document.styleSheets[i].title==mySheet){document.styleSheets[i].disabled=false;change=true;}}if(!change)document.styleSheets[oldSheet].disabled=false;return change;}function sheet(){var sheetName = prompt("Stylesheet Name?");if(!ssEnable(sheetName)){alert("Not found - original stylesheet retained.");}}window.onload=function(){document.getElementById('btn1').onclick=sheet;}</script></head><body>Switch my Stylesheet with the button below!<br/><input type="button" id="btn1" value="Change Sheet"/></body></html>我发现了if(!change)document.styleSheets[oldSheet].disabled=false;return change;如果将代码加上{}中,是会弹出提示框alert("Not found - original stylesheet retained.");但是按照书中写是没有,为什么呢?求各位指点指点,谢谢如果把if(!change)document.styleSheets[oldSheet].disabled=false;return change;写成:if(!change){document.styleSheets[oldSheet].disabled=false;return change;}就会弹框,而样式也改变了
添加回答
举报
0/150
提交
取消