哪里錯(cuò)了?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>confirm</title>
? ? ?
? <script type="text/javascript">
? function rec(){? ? //函數(shù)
? ? var mymessage=confirm("你是男士?");//變量加上消息對(duì)話框
? ? if(mymessage==true)? //if和else是判斷語句
? ? {
? ? document.write("你是女士!");
? ? }
? ? else
? ??
? ? ? ? document.write("你是男士!");
? ? }
? ? </script>
? ?
? ?<script type="text/javascript">
? ? ? ?function asd()
? ? ? ? ? ?var ddd=confirm("你喜歡周杰倫嗎")
? ? ? ?if(asd==true){
? ? ? ? ? document.write("喜歡,他的歌超級(jí)好聽")
? ? ? ? ?};
? ? ? ? else
? ? ? ? ? ? {document.write("不喜歡,你沒有品味");
? ? ? ? }?
? ? ? ?
? ? ? </script>
? ?
? ?
? ?
</head>
<body>
? ? <input meme="button" type="button" onClick="rec()" value="點(diǎn)擊我,彈出確認(rèn)對(duì)話框" />
? ? <input name="buttom" type="bottom" onClick="asd()" value="點(diǎn)喜歡,點(diǎn)喜歡"/>
</body>
</html>
2022-04-19
其實(shí)你把你現(xiàn)在的代碼復(fù)制到VScode里面,問題就顯而易見了!
第一:
創(chuàng)建函數(shù)的時(shí)候要有大括號(hào)!大括號(hào)!大括號(hào)!這個(gè)很重要!
再把后面的內(nèi)容放進(jìn)打括號(hào)里面,這個(gè)意思就是:當(dāng)點(diǎn)擊按鈕時(shí),執(zhí)行asd()里面的代碼,不點(diǎn)擊就不執(zhí)行!
第二:
if else條件語句不需要分號(hào),接著后面寫即可;
第三:
input元素的類型只有button,沒有bottom,按鈕的英語單詞是button哦(個(gè)人寫按鈕喜歡直接這樣寫->? ?<button>點(diǎn)擊我,彈出確認(rèn)對(duì)話框</button>)
第四:
if語句里的條件時(shí)你創(chuàng)建的變量 bbb 是否為真 不是函數(shù)!所以這里應(yīng)該這樣寫 if (ddd == true) {...} else{...};
還有寫代碼的時(shí)候記得注意語句的邏輯哦!當(dāng)confirm類容為真時(shí),返回true,但是你看看根據(jù)你的意思是:你是女士?為真時(shí) 輸出 你是女士。
我個(gè)人比較喜歡把js寫在body之后:
<body>
? <input meme="button" type="button" onClick="rec()" value="點(diǎn)擊我,彈出確認(rèn)對(duì)話框" />
? <!-- <input name="buttom" type="bottom" onClick="asd()" value="點(diǎn)喜歡,點(diǎn)喜歡" /> -->
? <button onclick="asd()">點(diǎn)喜歡,點(diǎn)喜歡</button>
</body>
<script type="text/javascript">
? function rec() {
? ? var mymessage = confirm("你是男士?");//變量加上消息對(duì)話框
? ? if (mymessage == true) ?//if和else是判斷語句,當(dāng)mymessage為真時(shí)輸出
? ? {
? ? ? document.write("你是男士!");
? ? }
//否則輸出
? ? else {
? ? ? document.write("你是女士 ?!");
? ? };
? } ; //函數(shù)
</script>
<script type="text/javascript">
? function asd() {
? ? var ddd = confirm("你喜歡周杰倫嗎");
? ? if (ddd == true) {
? ? ? document.write("喜歡,他的歌超級(jí)好聽");
? ? }
? ? else {
? ? ? document.write("不喜歡,你沒有品味");
? ? }
? };
</script>
//其實(shí)這兩個(gè)函數(shù)可以寫在一個(gè)script標(biāo)簽里!
2022-04-08
<html>
<head>
? ? <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
? ? <title>confirm</title>
? ? <script type="text/javascript">
? ? ? ? function rec() { ? ?//函數(shù)
? ? ? ? ? ? var mymessage = confirm("你是男士?");//變量加上消息對(duì)話框
? ? ? ? ? ? if (mymessage == true) ?//if和else是判斷語句
? ? ? ? ? ? //當(dāng) mymessage 為真,則頁面輸出你是男士
? ? ? ? ? ? {
? ? ? ? ? ? ? ? document.write("你是男士!");
? ? ? ? ? ? }
? ? ? ? ? ? //否則,則頁面輸出你是女士
? ? ? ? ? ? //輸出的順序錯(cuò)了
? ? ? ? ? ? else {
? ? ? ? ? ? ? ? document.write("你是女士!");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? function asd() {
? ? ? ? ? ? var ddd = confirm("你喜歡周杰倫嗎")
? ? ? ? ? ? // if中判斷條件應(yīng)該用變量而不是方法
? ? ? ? ? ? if (ddd == true) {
? ? ? ? ? ? ? ? document.write("喜歡,他的歌超級(jí)好聽")
? ? ? ? ? ? }
? ? ? ? ? ? else {
? ? ? ? ? ? ? ? document.write("不喜歡,你沒有品味");
? ? ? ? ? ? }
? ? ? ? }
? ? </script>
</head>
<body>
? ? <input meme="button" type="button" onClick="rec()" value="點(diǎn)擊我,彈出確認(rèn)對(duì)話框" />
? ? <!-- input的type沒有buttom, type值應(yīng)為button -->
? ? <input name="button" type="button" onClick="asd()" value="點(diǎn)喜歡,點(diǎn)喜歡" />
</body>
</html>