将 JavaScript 结果打印到两个 DIV 而不是输入中

Print JavaScript results into two DIVs instead of Inputs

本文关键字:DIV 两个 输入 结果 JavaScript 打印      更新时间:2023-09-26

大家晚上好,

我想创建一个从出生日期计算占星术标志的函数,在两个不同的 DIV 中打印结果。到目前为止,我只能将它们打印成两个输入,如下所示:

<head>
    <script language="javascript">
        function signs() {
            var start = 1901, birthyear = "2000", date = "04", month = "05";
            with (document.zodiac.sign){
            if ((month == 1 || month == 01) && date >=20 || (month == 2 || month == 02) && date <=18) {value = "♒ Verseau";}
            if ((month == 1 || month == 01) && date > 31) {value = "Cette date n'existe pas.";}
            if ((month == 2 || month == 02) && date >=19 || (month == 3 || month == 03) && date <=20) {value = "♓ Poisson";}
            if ((month == 2 || month == 02) && date > 29) {value = "Cette date n'existe pas.";}
            if ((month == 3 || month == 03) && date >=21 || (month == 4 || month == 04) && date <=19) {value = "♈ Bélier";}
            if ((month == 3 || month == 03) && date > 31) {value = "Cette date n'existe pas.";}
            if ((month == 4 || month == 04) && date >=20 || (month == 5 || month == 05) && date <=20) {value = "♉ Taureau";}
            if ((month == 4 || month == 04) && date > 30) {value = "Cette date n'existe pas.";}
            if ((month == 5 || month == 05) && date >=21 || (month == 6 || month == 06) && date <=21) {value = "♊ Gémeaux";}
            if ((month == 5 || month == 05) && date > 31) {value = "Cette date n'existe pas.";}
            if ((month == 6 || month == 06) && date >=22 || (month == 7 || month == 07) && date <=22) {value = "♋ Cancer";}
            if ((month == 6 || month == 06) && date > 30) {value = "Cette date n'existe pas.";}
            if ((month == 7 || month == 07) && date >=23 || (month == 8 || month == 08) && date <=22) {value = "♌ Lion";}
            if ((month == 7 || month == 07) && date > 31) {value = "Cette date n'existe pas.";}
            if ((month == 8 || month == 08) && date >=23 || (month == 9 || month == 09) && date <=22) {value = "♍ Vierge";}
            if ((month == 8 || month == 08) && date > 31) {value = "Cette date n'existe pas.";}
            if ((month == 9 || month == 09) && date >=23 || month == 10 && date <=22) {value = "♎ Balance";}
            if ((month == 9 || month == 09) && date > 30) {value = "Cette date n'existe pas.";}
            if (month == 10 && date >=23 || month == 11 && date <=21) {value = "♏ Scorpion";}
            if (month == 10 && date > 31) {value = "Cette date n'existe pas.";}
            if (month == 11 && date >=22 || month == 12 && date <=21) {value = "♐ Sagittaire";}
            if (month == 11 && date > 30) {value = "Cette date n'existe pas.";}
            if (month == 12 && date >=22 || (month == 1 || month == 01) && date <=19) {value = "♑ Capricorne";}
            if (month == 12 && date > 31) {value = "Cette date n'existe pas.";}
            }
            x = (start - birthyear) % 12
            with (document.zodiac.csign){
            if (x == 1 || x == -11) {value = "鼠 Rat";}
            if (x == 0) {value = "牛 Bœuf";}
            if (x == 11 || x == -1) {value = "兎 Tigre";}
            if (x == 10 || x == -2) {value = "兔 Lapin";}
            if (x == 9 || x == -3)  {value = "龍 Dragon";}
            if (x == 8 || x == -4)  {value ="蛇 Serpent";}
            if (x == 7 || x == -5)  {value = "馬 Cheval";}
            if (x == 6 || x == -6)  {value = "羊 Mouton";}
            if (x == 5 || x == -7)  {value = "猴 Singe";}
            if (x == 4 || x == -8)  {value = "雞 Coq";}
            if (x == 3 || x == -9)  {value = "狗 Chien";}
            if (x == 2 || x == -10)  {value = "豬 Cochon";}
            }
        }
    </script>
</head>
<body onload="signs()">
    <form name="zodiac">
        <table>
            <tr>
                <td>Signe astrologique chinois :</td>
                <td><input type="text" name="csign" size="12"></td>
            </tr>
            <tr>
                <td>Signe du zodiaque :</td>
                <td><input type="text" name="sign" size="12" value="" align="right"></td>
            </tr>
        </table>
    </form>
</body>

我尝试创建新的 DIV 而不是输入,并在符号csign(第 6 行和第 33 行)之后添加 innerHTML,但它不起作用。

谁能帮我找到如何进行?提前谢谢你!

不要使用 with : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with

通过 id 访问您的div。

http://jsfiddle.net/ymeejLzb/