HTML - 按钮和输入

标签 html css

我正在用 HTML 创建一个网页,每当我点击其中一个按钮时,它就会清除我输入中的所有文本。当我按下其中一个按钮时,我只希望它清除其 div 中的文本。我该怎么做呢?这是我的 html 代码:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>SmartMail</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="container">
    <h1><b>Welcome to SmartMail!!</b></h1>
    <form>
      <div class="recipients">
        <input type="text" class="typer">
        <br><br>
        <button class="ar">Add User</button>
        <button class="ur">Remove User</button>
        <br><br><br>
        <select name="users" class="userlist" size="24">
          <option class="listhead"> __________*Recipents*__________
          </option>
          <option class="listhead">-------------------------------
          </option>
        </select>
      </div>
      <div class="content">
        <button class="send">Send</button>
        <br><br>
        <textarea name="content" class="typec" cols="113" rows="12"> 
                    </textarea>
      </div>
    </form>
  </div>
</body>

</html>

任何帮助都是有用的!谢谢!

最佳答案

当您点击任何按钮时,您的页面总是会刷新,因为 button form 内的标签标记被解释为提交按钮 ( <button type="submit"> )。你需要明确地给按钮一个类型 button .您还需要防止在 input 中按下 enter 时的默认操作与 event.preventDefault() .

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SmartMail</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <h1><b>Welcome to SmartMail!!</b></h1>
        <form>
            <div class="recipients">
                <input type="text" class="typer" id="typer">
                <br><br>
                <button type="button" class="ar">Add User</button>
                <button type="button" class="ur">Remove User</button>
                <br><br><br>
                <select name="users" class="userlist" size="24">
                    <option class="listhead"> __________*Recipents*__________ 
                    </option>
                    <option class="listhead">------------------------------- 
                    </option>
                </select>
            </div>   
            <div class="content">
                <button type="button" class="send">Send</button>
                <br><br>
                <textarea name="content" class="typec" cols="113" rows="12"> 
                </textarea>
            </div>
        </form>
    </div>
    <script>
    document.getElementById('typer').addEventListener("keypress", (e)=>{
      if(e.keyCode==13){
       e.preventDefault();
      }
    });
    </script>
</body>
</html>

关于HTML - 按钮和输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51559625/

相关文章:

javascript - 无需重新加载即可更改 URL

html - 为什么宽度为 960px?

javascript - 如何根据元素的宽度改变背景颜色?

javascript - 在移动设备或桌面导航栏时显示汉堡菜单

javascript - 菜单打开时移动 View 问题?

我可以使用 Java API 操作和修改 HTML 代码及其标签?

html - 空白 : nowrap not working in IE 9

css - body 元素上指定的字体大小被输入忽略

CSS - 覆盖继承的文本装饰 :line-through

css - 组合 CSS nth-child 函数