【问题标题】:Javascript, GetElementById element is not defined? [duplicate]Javascript,GetElementById 元素没有定义? [复制]
【发布时间】:2021-12-25 05:05:04
【问题描述】:

由于某些原因,我无法使用 javascript 显示#coupon,当我尝试获取 elementById 时,它会显示错误消息 elementById undefined,

HTML

<html>

<head>
  <title>Exercise</title>
  <link rel="stylesheet" href="stylesheet.css">
  <script src="javascript.js"></script>
</head>

<body>

  <div class="inputi">
    <p class="input-text">Une Jam?</p> <input placeholder="Sheno emrin..." type="text" id="inputId">

    //checks input value
    <button type="button" onclick="getInputValue()">Vazhdo</button>
    // shows error message if it doesn't match the array
  <p id="message"></p>
  </div>


    // i want to display this with javascript, after the login
    <h1 id="coupon">You won giftcard </strong></h1>
    <button id="coupon">abas</button>

 


</body>

</html>

JS(问题)

** 问题 getElementById 未定义,我想在成功登录尝试后显示:阻止。 **

      var zzz = getElementById("coupon")
      if(zzz === "none") {
      display:block; }
 

    
      
      // document.writeln("<h1>Keni fituar kupon 50% ne <strong> Green & Protein </strong> </h1>");
      
      // document.writeln('<input class="giftcode" placeholder="' +finalcode+'" type="text" id="inputId">');
      display_image(images[z], 400, 400, 'Javascript');
      document.write('<br>' + user[i]);

    }
  }

}

【问题讨论】:

  • 不是您遇到的主要问题,但id 在文档中应该是唯一的。您有两个具有相同 coupon id 的元素。

标签: javascript html css dom


【解决方案1】:

试试:document.getElementById 而不是:getElementById

因为 getElementById 是一个文档方法

【讨论】:

    【解决方案2】:

    从上面的代码中可以看出,您正在尝试调用尚未定义的方法 getElementById()。

    第二,您只能在文档中分配 1 个 id,否则它将不起作用。

    你可以这样做

    //我想在登录后用javascript显示这个

    <span id="coupon" style="display: none">
       <h1>You won giftcard </strong></h1>
       <button>abas</button>
    </span>
    

    你的意思是 document.getElementById("coupon");这是正确的功能。

    所以试试这个;

    var zzz = document.getElementById("coupon");

    我真的不知道您成功登录的标准是什么,但这个显示帮助 假设您在成功登录时将变量设置为var success = "yes"

    if(success === "yes"){
    zzz.style.display = "block";
    }
    

    【讨论】:

      【解决方案3】:

      两件事:

      • getElementById 应该是document.getElementById

      • 您需要将您的脚本标签移动到body标签的底部,以确保您选择该元素时该元素在DOM中

      用途:

      <html>
         <head>
            <title>Exercise</title>
            <link rel="stylesheet" href="stylesheet.css">
         </head>
         <body>
            <div class="inputi">
               <p class="input-text">Une Jam?</p>
               <input placeholder="Sheno emrin..." type="text" id="inputId">
               //checks input value
               <button type="button" onclick="getInputValue()">Vazhdo</button>
               // shows error message if it doesn't match the array
               <p id="message"></p>
            </div>
            // i want to display this with javascript, after the login
            <h1 id="coupon">You won giftcard </h1>
            <button id="coupon">abas</button>
            <script src="javascript.js"></script>
         </body>
      </html>
      

      【讨论】:

      • var element = document.getElementById("coupon"); if( element.style.display === " none") { element.style.display = "block" } // 这仍然给我一个错误
      • @samgear 无法重现该错误。确保您已将script 标记移至body 的底部。请参阅我的答案中的代码块。
      【解决方案4】:

      【讨论】:

        猜你喜欢
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多