当前位置:首页 > JavaScript

JS实现inpubox

2026-04-07 05:35:18JavaScript

实现 InputBox 的基本结构

使用 HTML 创建一个简单的输入框结构,包含一个输入框和一个按钮。

<div class="input-box">
  <input type="text" id="inputField" placeholder="Enter text here">
  <button id="submitBtn">Submit</button>
</div>

添加样式

通过 CSS 美化输入框和按钮,使其更具交互性。

.input-box {
  display: flex;
  gap: 10px;
  margin: 20px;
}

#inputField {
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  width: 200px;
}

#submitBtn {
  padding: 8px 16px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

#submitBtn:hover {
  background-color: #0056b3;
}

实现交互功能

使用 JavaScript 添加事件监听器,处理用户输入和按钮点击。

JS实现inpubox

document.getElementById('submitBtn').addEventListener('click', function() {
  const inputValue = document.getElementById('inputField').value;
  if (inputValue.trim() !== '') {
    alert('You entered: ' + inputValue);
  } else {
    alert('Please enter some text');
  }
});

添加键盘事件

为输入框添加键盘事件,允许用户通过按 Enter 键提交内容。

document.getElementById('inputField').addEventListener('keypress', function(event) {
  if (event.key === 'Enter') {
    document.getElementById('submitBtn').click();
  }
});

动态创建 InputBox

通过 JavaScript 动态创建 InputBox 元素,适用于需要动态添加输入框的场景。

JS实现inpubox

function createInputBox() {
  const container = document.createElement('div');
  container.className = 'input-box';

  const input = document.createElement('input');
  input.type = 'text';
  input.placeholder = 'Enter text here';
  input.id = 'dynamicInput';

  const button = document.createElement('button');
  button.textContent = 'Submit';
  button.id = 'dynamicBtn';

  container.appendChild(input);
  container.appendChild(button);

  document.body.appendChild(container);

  button.addEventListener('click', function() {
    const value = input.value;
    if (value.trim() !== '') {
      console.log('Submitted:', value);
    }
  });
}

createInputBox();

验证输入内容

添加输入验证逻辑,确保用户输入符合特定要求。

document.getElementById('submitBtn').addEventListener('click', function() {
  const inputValue = document.getElementById('inputField').value;
  if (inputValue.length < 5) {
    alert('Input must be at least 5 characters long');
  } else {
    console.log('Valid input:', inputValue);
  }
});

使用事件委托

对于动态生成的多个输入框,使用事件委托优化事件处理。

document.body.addEventListener('click', function(event) {
  if (event.target.classList.contains('dynamic-btn')) {
    const input = event.target.previousElementSibling;
    console.log('Submitted:', input.value);
  }
});

标签: JSinpubox
分享给朋友:

相关文章

JS如何实现左右滑动

JS如何实现左右滑动

实现左右滑动的方法 使用 touchstart、touchmove 和 touchend 事件监听触摸操作,计算滑动距离和方向。 let startX, moveX; element.addEve…

JS节流和防抖的实现

JS节流和防抖的实现

节流(Throttle)的实现 节流是指在一定时间间隔内,函数最多执行一次。常用于限制高频事件的触发频率,如滚动事件、窗口调整等。 function throttle(func, delay) {…

JS实现一个并发函数

JS实现一个并发函数

实现并发控制的几种方法 在JavaScript中实现并发控制可以通过多种方式,以下是几种常见的方法: Promise.all 使用Promise.all可以并行执行多个异步任务,等待所有任务完成后再…

实现 slice JS

实现 slice JS

实现 JavaScript 的 slice 方法 JavaScript 的 slice 方法用于从数组或字符串中提取一部分,返回一个新的数组或字符串,而不修改原始数据。以下是实现 slice 的核心逻…

动态实现JS

动态实现JS

动态实现 JavaScript 的方法 动态加载脚本 通过创建 script 元素并插入到 DOM 中实现动态加载。例如: const script = document.createEleme…

JS实现取色

JS实现取色

使用 input[type="color"] 获取颜色值 HTML5 提供了原生颜色选择器,通过 <input type="color"> 可直接调用系统取色工具。用户选择的颜色会以十六进…