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