為什么按鈕點(diǎn)擊都沒(méi)有效果啊?以下是我的代碼。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無(wú)標(biāo)題文檔</title>
<style type="text/css">
body{font-size:12px;}
#txt{
? ? height:400px;
? ? width:600px;
border:red solid 1px;
padding:5px;}
p{
line-height:18px;
text-indent:2em;}
</style>
</head>
<body>
? ?<script type="text/javascript">
//定義"取消設(shè)置"的函數(shù)
function init4(){
? ? var stu = documnet.getElementById("con");
? ? stu = confirm("確定設(shè)置");
? ? if(stu == true){
? ? document.write("確定設(shè)置,很好!"); ? ?
? ? }
? ? else{
? ? ? ? document.write("null");
? ? }
}
//定義"改變顏色"的函數(shù)
function init(){
? ? var one1 = document.getElementById("one");
? ? one1.style.color = "blue";
? ? one1.style.backgroundColor = "#CCC";
}//定義"改變寬高"的函數(shù)
function init1(){
? ? var two1 = document.getElementById("two");
? ? two1.style.width = "200px";
? ? two1.style.height = "100px";
}
//定義"隱藏內(nèi)容"的函數(shù)
function init2(){
? ? var three1 = document.getElementById("three");
? ? three1.style.display = "none";
}
//定義"顯示內(nèi)容"的函數(shù)
function init3(){
? ? var four1 = document.getElementById("four");
? ? four1.style.display = "block";
}
? </script>
?<h1 id="con">JavaScript課程</h1>
? <div id="txt">?
? ? ?<h2 id="one">JavaScript為網(wǎng)頁(yè)添加動(dòng)態(tài)效果并實(shí)現(xiàn)與用戶交互的功能。</h2>
? ? ? ? <p id="two">1. JavaScript入門篇,讓不懂JS的你,快速了解JS。</p>
? ? ? ? <p id="three">2. JavaScript進(jìn)階篇,讓你掌握J(rèn)S的基礎(chǔ)語(yǔ)法、函數(shù)、數(shù)組、事件、內(nèi)置對(duì)象 、BOM瀏覽 器、DOM操作。</p>
? ? ? ? <p id="four">3. 學(xué)完以上兩門基礎(chǔ)課后,在深入學(xué)習(xí)JavaScript的變量作用域、事件、對(duì)象、運(yùn)動(dòng)、cookie、正則表達(dá)式、ajax等課程。</p>
? </div>
? ?<form>
? <!--當(dāng)點(diǎn)擊相應(yīng)按鈕,執(zhí)行相應(yīng)操作,為按鈕添加相應(yīng)事件-->
? ? <input type="button" value="改變顏色" onclick="init"> ?
? ? <input type="button" value="改變寬高" onclick="init1">
? ? <input type="button" value="隱藏內(nèi)容" onclick="init2">
? ? <input type="button" value="顯示內(nèi)容" onclick="init3">
? ? <input type="button" value="取消設(shè)置" onclick="init4">
? </form>
</body>
</html>
2019-01-05
一、改為onclick="init()",onclick="init1()",onclick="init2()",onclick="init3()",onclick="init4()"
二、函數(shù)init3()(顯示內(nèi)容的方法),你針對(duì)id=four的p標(biāo)簽進(jìn)行操作,但是它本來(lái)就是顯示的,你可以改為
????????????????????var four1 = document.getElementById("three");
????????在對(duì)id為three的p標(biāo)簽進(jìn)行隱藏后在使用這個(gè)按鈕就可以顯示內(nèi)容了
三、函數(shù)init4()看不懂,不知道你想怎么操作
2019-01-05
你的input標(biāo)簽里面的 onclick 屬性 少寫括號(hào)“()”你再改了試試
另外不知道是不是你故意這么寫的,但是你現(xiàn)在的代碼邏輯并不能實(shí)現(xiàn)正確的功能,建議再檢查檢查。
祝你寫代碼開(kāi)心~