我有一个表单,当我在表单上附加另一个字段时,无法在其他函数中使用它。

<!DOCTYPE html>
<html>
  <head>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
     <script>
        $(document).ready(function(){
          $(".registerNums").click(function(){
              $("#hello").append('<input type="button" value="register" class="inputMems">');
          });
          $(".inputMems").click(function(){
             alert("hi")
         });
       });
   </script>
  </head>
  <body>
    <div>
      <form>
        <div id="hello"></div>
        <input type="button" value="register mems" class="registerNums">
      </form>
    </div>
  </body>
</html>

看我的inputMems按钮不起作用:http://jsfiddle.net/Yelesee/3uecqLxn/

最佳答案

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(".registerNums").click(function(){
        $("#hello").append('<input type="button" value="register" class="inputMems">');
    });
    $("#hello").on('click','.inputMems',function(){console.log("hi");});
});
</script>
</head>
<body>

<div>
<form>
<div id="hello"></div>

<input type="button" value="register mems" class="registerNums">
</form>
</div>

</body>
</html>

关于javascript - html标记无法在Javascript中使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51765141/

10-11 12:58