l文件中添加一個(gè)表單,用于用戶輸入出生年份。代碼如下:
```>
<label for="birth-year">請(qǐng)輸入出生年份:</label>putame="birth-year">it>>
2. 接下來(lái),我們需要添加一些JavaScript代碼,用于計(jì)算用戶的屬相和星座。代碼如下:
<script>ction calculate() {ententById("birth-year").value;
var zodiacs = ["猴", "雞", "狗", "豬", "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊"];dex = (year - 4) % 12;dex];stellations = ["水瓶座", "雙魚座", "白羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蝎座", "射手座", "摩羯座"];stellationIndex = ((year % 100) - 4) % 12;stellationstellationsstellationIndex];stellation);
}enttListeneritctiont) {ttDefault();
calculate();
});
</script>
3. 最后,我們將查詢結(jié)果顯示在頁(yè)面上。代碼如下:
<div id="result"></div>
修改calculate函數(shù),將結(jié)果顯示在這個(gè)div中:
```ction calculate() {ententById("birth-year").value;
var zodiacs = ["猴", "雞", "狗", "豬", "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊"];dex = (year - 4) % 12;dex];stellations = ["水瓶座", "雙魚座", "白羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蝎座", "射手座", "摩羯座"];stellationIndex = ((year % 100) - 4) % 12;stellationstellationsstellationIndex];ententByIdnerHTMLstellation;
至此,我們已經(jīng)完成了一個(gè)簡(jiǎn)單的屬相星座查詢頁(yè)面。用戶輸入出生年份,點(diǎn)擊查詢按鈕后,頁(yè)面會(huì)顯示用戶的屬相和星座。