【问题标题】:How to connect a id to a trigger function in html and javascript如何将 id 连接到 html 和 javascript 中的触发函数
【发布时间】:2020-08-05 21:48:01
【问题描述】:

通过下面的代码,我试图让它响应静音和静音的输入。因此,当在文本框中输入静音或静音时,它会将颜色更改为红色(静音)和绿色(静音),在这种情况下,链接 ID 为 id="text" 和 "text1"。谢谢!

HTML:

  <!DOCTYPE html>
    <html>
    <body>

    <p>Write something in the text field to trigger a function.</p>

    <input type="text" id="myInput" oninput="myFunction()">

    <p id="demo"></p>

    <script>
    function myFunction() {
      var x = document.getElementById("myInput").value;
      document.getElementById("demo").innerHTML = "You wrote: " + x;
    }
    </script>

    </body>
    </html>

想要链接的 HTML:

<h1 class="l1-txt1 txt-center p-t-0 p-b-10">
    <p id="text1" style="color:white; font-weight: 600"></p>
</h1>

<h1 class="l1-txt1 txt-center p-t-0 p-b-60">
    <p id="text" style="color:crimson; font-weight: 600"></p>
</h1>

【问题讨论】:

    标签: javascript html css telnet


    【解决方案1】:

    您可以尝试以下方法:

    function myFunction() {
      var x = document.getElementById("myInput").value;
      var t1 = document.getElementById("text1");
      var t2 = document.getElementById("text");
      document.getElementById("demo").innerHTML = "You wrote: " + x;
      if(x.trim().toLowerCase() == 'mute'){
        t1.style.color = 'green';
        t2.style.color = 'green';
      }
      else if(x.trim().toLowerCase() == 'muteon'){
        t1.style.color = 'red';
        t2.style.color = 'red';
      }
    }
    <p>Write something in the text field to trigger a function.</p>
    
    <input type="text" id="myInput" oninput="myFunction()">
    
    <p id="demo"></p>
    <h1 class="l1-txt1 txt-center p-t-0 p-b-10">
      <p id="text1" style="color:white; font-weight: 600">Text 1</p>
    </h1>
    
    <h1 class="l1-txt1 txt-center p-t-0 p-b-60">
      <p id="text" style="color:crimson; font-weight: 600">Text</p>
    </h1>

    【讨论】:

    • 这正是我想要的。我会玩弄它,如果我有任何问题,我会告诉你。谢谢!
    • 如何让文本框由来自和外部源的输入填充,或者在我的情况下,我希望通过 telnet 远程填充文本框。你知道我怎么能做到这一点
    • @StackGuru,我没有任何远程登录经验。您可以提出一个新问题来指定您的要求,可能会有人可以帮助您:)
    猜你喜欢
    • 1970-01-01
    • 2012-07-24
    • 2018-11-30
    • 2023-04-02
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    相关资源
    最近更新 更多