【问题标题】:how to change html script tag to jsx code?如何将 html 脚本标签更改为 jsx 代码?
【发布时间】:2022-01-11 07:48:26
【问题描述】:

在将其转换为 jsx 时遇到问题,我知道 html 中的标签本身代表 jsx,但是当我将它添加到一个 .js 的新文件中时,它只是将我的代码作为文本读取?它只是在网页上将我的代码显示为文本 (此代码适用于 html 页面,它是一个输入框,用户可以在其中输入电子邮件以获取时事通讯)

<html>
<body>
   ...styling code elements
   ...styling code elements

<button  class="join" id = "join"> join </button>
  
        <script>
            let email = document.getElementById("email")
            document.getElementById("join").onclick = () => {
                console.log(email.value)
                fetch("/sendemail?email=" + email.value, {
                    method: "POST"
                }).then((res) => {
                    // success
                    alert("success")
                }).catch((err) => {
                    // error 
                    alert(err)
                })
            }
        </script>
   </body>
</html>

【问题讨论】:

    标签: javascript html reactjs jsx


    【解决方案1】:

    您需要使用 src 属性将您的脚本 HTML 元素标记指向 .js 文件。然后,将脚本标记之间的文本放入 .js 文件本身。

    &lt;script src="main.js"&gt; &lt;/script&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2019-07-05
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多