Home > Web Front-end > JS Tutorial > body text

How to implement date picker function using JavaScript?

WBOY
Release: 2023-10-20 16:54:19
Original
1230 people have browsed it

如何使用 JavaScript 实现日期选择器功能?

How to use JavaScript to implement the date picker function?

[Introduction]
Date picker is one of the common functions in web development, which can provide users with a convenient and fast way to select dates. This article explains how to implement a simple date picker using JavaScript and provides detailed code examples.

[Implementation steps]

1. HTML structure
First, add an input box and a button to the HTML to trigger the date picker pop-up window.

<input type="text" id="dateInput">
<button type="button" onclick="showDatePicker()">选择日期</button>
Copy after login

2. CSS Style
In order to make the date picker pop-up window display beautifully, you can add some styles to it.

#datePicker {
  position: absolute;
  background-color: #fff;
  padding: 10px;
  border: 1px solid #ccc;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
  z-index: 9999;
}
Copy after login

3. JavaScript implementation
Next, use JavaScript to implement the date picker function.

// 获取日期选择器弹窗和日期输入框元素
var datePicker = document.createElement("div");
datePicker.setAttribute("id", "datePicker");
var dateInput = document.getElementById("dateInput");

// 显示日期选择器弹窗
function showDatePicker() {
  // 获取输入框位置和大小
  var rect = dateInput.getBoundingClientRect();
  
  // 设置日期选择器弹窗位置
  datePicker.style.left = rect.left + "px";
  datePicker.style.top = (rect.top + rect.height) + "px";
  
  // 清空日期选择器弹窗内容
  datePicker.innerHTML = "";
  
  // 创建日期选择器表格
  var table = document.createElement("table");
  
  // 创建表头
  var thead = document.createElement("thead");
  var tr = document.createElement("tr");
  var th = document.createElement("th");
  th.textContent = "日";
  tr.appendChild(th);
  // ...省略其他表头代码...
  thead.appendChild(tr);
  table.appendChild(thead);
  
   // 创建日期选择器内容(示例为一个月的数据)
  var tbody = document.createElement("tbody");
  for (var i = 1; i <= 30; i++) {
    var tr = document.createElement("tr");
    for (var j = 1; j <= 7; j++) {
      var td = document.createElement("td");
      td.textContent = i + (j - 1) * 7;
      tr.appendChild(td);
    }
    tbody.appendChild(tr);
  }
  table.appendChild(tbody);
  
  // 将日期选择器表格添加到弹窗
  datePicker.appendChild(table);
  
  // 将日期选择器弹窗添加到页面
  document.body.appendChild(datePicker);
}

// 隐藏日期选择器弹窗
function hideDatePicker() {
  // 判断点击事件是否在日期选择器内部,是则不隐藏
  var clickInside = false;
  var e = window.event;
  var target = e.target;
  while (target.parentNode) {
    if (target.parentNode === datePicker) {
      clickInside = true;
      break;
    }
    target = target.parentNode;
  }
  if (!clickInside) {
    document.body.removeChild(datePicker);
  }
}

// 点击页面其他地方时隐藏日期选择器弹窗
document.addEventListener("click", hideDatePicker);
Copy after login

[Usage]
Copy the above code into an HTML file and save it, then open the file in the browser. Click the "Select Date" button and a date picker pop-up window will pop up. Click elsewhere on the page and the pop-up window will automatically hide.

[Summary]
It is not difficult to implement a simple date picker function through JavaScript. You only need to dynamically create elements, set styles, and event listeners. Developers can customize and extend the date picker according to actual needs to make it more suitable for project needs. Remember, ensuring code readability and maintainability and improving user experience are one of the important goals in the development process.

The above is the detailed content of How to implement date picker function using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template