點(diǎn)擊所有按鈕都沒反應(yīng)
? <!--當(dāng)點(diǎn)擊相應(yīng)按鈕,執(zhí)行相應(yīng)操作,為按鈕添加相應(yīng)事件-->
? ? <input type="button" value="改變顏色" onclick="changeColor()" > ?
? ? <input type="button" value="改變寬高" onclick="changeWH()">
? ? <input type="button" value="隱藏內(nèi)容" onclick="txtHide()">
? ? <input type="button" value="顯示內(nèi)容" onclick="txtOn()">
? ? <input type="button" value="取消設(shè)置" onclick="cancel()">
? </form>
? <script type="text/javascript">
? var obj=document.getElementById("txt");
//定義"改變顏色"的函數(shù)
function chageColor()
{
? ? obj.style.color="red";
}
//定義"改變寬高"的函數(shù)
function changeWH()
{
? ? obj.style.height="300px";
? ? obj.style.width="500px";
}
//定義"隱藏內(nèi)容"的函數(shù)
function txtHide()
{
? ? obj.style.display="none";
}
//定義"顯示內(nèi)容"的函數(shù)
function txtOn()
{
? ? obj.style.display="block";
}
//定義"取消設(shè)置"的函數(shù)
funtion cancel()
{
? ? if(confirm("是否取消所有設(shè)置"))
? ? obj.className=txt;
}
? </script>
</body>
</html>
2018-08-17
首先你的changeColor()函數(shù)拼錯了,少個n,其次你的cancel函數(shù)前面的function也拼錯了,少個c。
然后所有按鈕都點(diǎn)不動的話是因?yàn)樵趕cript代碼塊中寫進(jìn)了中文格式的符號。
最后cancel()要實(shí)現(xiàn)恢復(fù)初始樣式的話網(wǎng)上搜索到兩種方法
1:obj.removeAttribte("style");
2:? txt.style.cssText="";
至于你寫的方法,我想你的初衷是將obj的樣式設(shè)置為style中寫好的“#txt”這一樣式 ,如果這個樣式名叫“.txt”,那就可以使用裝載類名的方法(obj.className="txt";你的沒有加雙引號),但是”#txt"是和id叫txt的元素綁定的初始樣式 ,它們是一體的,后期對txt元素樣式的修改就是對#txt的修改,所以你的cancel做的工作就是把當(dāng)前樣式設(shè)置為當(dāng)前樣式?,所以是完全不起作用的;? ????????????????????????????????????????????????????????????????????????????????????????
2018-08-07
2018-08-04
因?yàn)槟愕膐bj對象沒有賦值,
比如:
var obj = document.getElementById("con");
obj.style.color="blue";
你這樣試試
上面的con指的是標(biāo)簽的id
如:
<p id="con"></p>