运用HTML和js知识写一串代码,要求:计算年龄:弹出一个输入框,输入出生年份后,计算出年龄并显示。
时间: 2023-06-02 17:06:54 浏览: 626
好的,这是一道比较基础的前端问题,以下是代码:
HTML部分:
```
<label for="birth-year">请输入出生年份:</label>
<input type="text" id="birth-year">
<button onclick="getAge()">计算年龄</button>
<p id="result"></p>
```
JavaScript部分:
```
function getAge() {
const birthYear = document.getElementById('birth-year').value;
const nowYear = new Date().getFullYear();
const age = nowYear - birthYear;
document.getElementById('result').innerHTML = `您现在的年龄是:${age}岁`;
}
```
这段代码将弹出一个输入框,要求输入出生年份,然后计算出年龄并显示在页面上。
相关问题
帮我写一个微信小程序弹出式的输入框,要求输入的是以元为单位的价格
好的,以下是一个简单的微信小程序弹出式的输入框,并且限制输入的是以元为单位的价格:
在WXML文件中添加以下代码:
```html
<view class="container">
<button bindtap="showModal">点击输入价格</button>
<!-- 弹出式的输入框 -->
<view class="modal" hidden="{{!modalShow}}">
<view class="modal-mask"></view>
<view class="modal-container">
<view class="modal-title">请输入价格(元)</view>
<input class="modal-input" type="number" step="0.01" placeholder="请输入价格" bindinput="inputPrice" />
<button class="modal-confirm" bindtap="confirmPrice">确定</button>
</view>
</view>
</view>
```
在对应的WXSS文件中添加以下样式:
```css
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
}
.modal {
position: fixed;
z-index: 9999;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.modal-mask {
position: absolute;
z-index: 1;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
}
.modal-container {
position: absolute;
z-index: 2;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
border-radius: 10px;
padding: 20px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
text-align: center;
}
.modal-title {
font-size: 16px;
font-weight: bold;
margin-bottom: 10px;
}
.modal-input {
width: 80%;
margin-bottom: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
text-align: center;
}
.modal-confirm {
display: block;
width: 80%;
margin: 0 auto;
padding: 10px;
background-color: #007aff;
color: #fff;
border-radius: 5px;
font-size: 16px;
}
```
在对应的JS文件中添加以下代码:
```javascript
Page({
data: {
modalShow: false, // 控制弹出层的显示与隐藏
price: '', // 输入的价格
},
// 点击按钮显示弹出层
showModal() {
this.setData({
modalShow: true,
});
},
// 输入价格
inputPrice(e) {
this.setData({
price: e.detail.value,
});
},
// 确定输入的价格
confirmPrice() {
// 判断输入的价格是否合法
if (/^[0-9]+(\.[0-9]{1,2})?$/.test(this.data.price)) {
// 合法的处理逻辑
wx.showToast({
title: `输入的价格为${this.data.price}元`,
icon: 'none',
});
// 关闭弹出层
this.setData({
modalShow: false,
price: '',
});
} else {
// 不合法的处理逻辑
wx.showToast({
title: '请输入正确的价格',
icon: 'none',
});
}
},
});
```
这样,一个微信小程序弹出式的输入框就完成了。用户点击按钮后会弹出一个输入框,输入的内容必须是以元为单位的价格,否则会提示用户重新输入。输入合法后,会弹出一个提示框显示输入的价格。
写一个html页面一个输入框 一个输出框,输入框输入unicode字符后自动输出字符串
好的,下面是代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Unicode字符输入输出页面</title>
</head>
<body>
<label for="input">输入Unicode字符:</label>
<input type="text" id="input" oninput="showOutput()"><br><br>
<label for="output">输出字符串:</label>
<textarea id="output"></textarea>
<script>
function showOutput() {
const input = document.getElementById("input").value;
const output = document.getElementById("output");
let str = "";
for (let i = 0; i < input.length; i+=4) {
str += String.fromCharCode(parseInt(input.substring(i, i+4), 16));
}
output.value = str;
}
</script>
</body>
</html>
```
这个页面包含一个输入框和一个输出框,当用户在输入框中输入Unicode字符时,页面会自动将其转换为对应的字符串并输出到输出框中。