孕周期计算器网页代码

创建一个简单的孕周期计算器网页代码可以使用HTML、CSS和JavaScript来实现。以下是一个基本的示例:

HTML部分

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>孕周期计算器</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <h1>孕周期计算器</h1>
        <label for="lastPeriodDate">请输入末次月经的第一天:</label>
        <input type="date" id="lastPeriodDate" name="lastPeriodDate">
        <button onclick="calculatePregnancy()">计算</button>
        <p id="result"></p>
    </div>
    <script src="script.js"></script>
</body>
</html>

CSS部分 (styles.css)

body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    margin: 0;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.container {
    background-color: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    text-align: center;
}

h1 {
    margin-bottom: 20px;
}

label {
    display: block;
    margin-bottom: 10px;
}

input[type="date"] {
    padding: 8px;
    border-radius: 4px;
    border: 1px solid #ccc;
    margin-bottom: 20px;
}

button {
    padding: 10px 20px;
    background-color: #007bff;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

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

#result {
    margin-top: 20px;
    font-size: 1.2em;
    color: #333;
}

JavaScript部分 (script.js)

function calculatePregnancy() {
    const lastPeriodDate = document.getElementById('lastPeriodDate').value;
    if (!lastPeriodDate) {
        alert('请输入末次月经的第一天!');
        return;
    }

    const today = new Date();
    const lastPeriod = new Date(lastPeriodDate);
    const timeDiff = today - lastPeriod;
    const daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
    const weeksPregnant = Math.floor(daysDiff / 7);
    const daysPregnant = daysDiff % 7;

    const result = `您已经怀孕 ${weeksPregnant} 周 ${daysPregnant} 天。`;
    document.getElementById('result').innerText = result;
}

说明

  1. HTML部分:创建了一个简单的表单,用户输入末次月经的第一天,点击按钮后计算孕周。
  2. CSS部分:美化页面,使其看起来更友好。
  3. JavaScript部分:计算从末次月经第一天到当前日期的天数,并将其转换为周和天。

使用方法

  1. 将HTML、CSS和JavaScript代码分别保存为index.htmlstyles.cssscript.js文件。
  2. 打开index.html文件即可在浏览器中使用孕周期计算器。

这个示例是一个简单的实现,实际应用中可能需要考虑更多细节,例如闰年、时区等。

所有内容均由人工智能模型生成,其生成内容的准确性和完整性无法保证,不代表我们的态度或观点。