javascript - onfocus 在 JavaScript 中不起作用

标签 javascript jquery html

我正在整理一些 javascript 表单验证,但我不知道如何让这个 onfocus 来控制我的 div...我只需要它来提醒我,我的 onfocus 函数正在与我的姓氏输入进行联系。 .. :(

html:

<div id="contact">
    <form name="form1" action="send.php" method="post" id="contact_f">
      <table id="contact_table">
        <tr>
          <td class="col1" colspan="2">
            <h1 class="center_text">Contact Aron</h1>
            <div id="error_messages">
              errors
            </div>
          </td>
        </tr>
        <tr>
          <td class="col1">
            <label for="first_name">First Name*</label><br>
            <input id="first_name" required name="Name" type="text" pattern="[a-zA-Z]+">
          </td>
          <td class="col2">
            <label for="last_name">Last Name*</label><br>
            <input id="last_name" required type="text">
          </td>
        </tr>
        <tr>
          <td class="col1">
            <label for="email">Email*</label><br>
            <input id="email" required type="email">
          </td>
          <td class="col2">
            <label for="confirm_email">Confirm Email*</label><br>
            <input id="confirm_email" required type="text">
          </td>
        </tr>
        <tr>
          <td class="col1">
            <label for="phone">Phone Number   <span id="color_gray">xxx-xxx-xxxx</span></label><br>
            <input id="phone" type="tel" pattern="\d{3}[\-]\d{3}[\-]\d{4}">
          </td>
          <td class="col2">

          </td>
        </tr>
        <tr class="col2">
          <td class="col1" colspan="2">
            <label for="message">Message*</label><br>
            <textarea id="message" required type="text"></textarea>
          </td>
        </tr>
        <tr>
          <td class="col1" colspan="2">
            <button id="submit_button" type="submit" value="submit">Submit Form</button>
          </td>
        </tr>
      </table>
  </form>

有问题的 JavaScript:

    document.getElementById("last_name").onfocus=function() {
      alert("last name");
    };

最佳答案

实际上你的代码运行良好。你可以使用 jquery 来尝试,就像这样 -

$(document).ready(function() {
$( "#last_name" ).focus(function() {
alert("Last name");
});  
});

关于javascript - onfocus 在 JavaScript 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34507278/

相关文章:

javascript - Bluebird 在 Node.js 上 promise : error and exception handling

javascript - 在指定的 div 之后使用函数删除 div

javascript - 模态不显示 Reactjs 使用 Hook

javascript - 在多维数组中查找元素并在其后面添加一些内容

javascript - 在 knockout 应用程序中使用 setInterval 进行轮询实现?

javascript - bootstrap 模式与 mixitup 可过滤组合冲突

javascript - 在脚本中接受图像

javascript - jQuery - 通过 "style"属性中的自定义 CSS 键查找元素的最佳方式

jquery 每个都有 json 对象

javascript jquery 选择单选按钮