当前位置:网站首页>js小练习----分时提醒问候、表单密码显示隐藏效果、文本框焦点事件、关闭广告
js小练习----分时提醒问候、表单密码显示隐藏效果、文本框焦点事件、关闭广告
2022-07-07 03:22:00 【鹿蹊zz】
目录
1.分时提醒问候
根据系统不同时间来判断,所以需要用到日期内置对象
上午打开页面就说:上午好,尊敬的用户! 0-12
下午打开页面就说:下午好,尊敬的用户!12-18
晚上打开页面就说:晚上好,尊敬的用户! 大于18
需要一个div元素,显示不同问候语,修改元素内容即可
<div>
<span id="greetTxt"></span>
<input type="button" name="" id="greet" value="显示问候" />
</div>
<script type="text/javascript">
// 1.获取元素
var greetTxt = document.getElementById('greetTxt');
var greet = document.getElementById('greet');
var str = '';
// 2. 得到当前的小时数
greet.onclick = function(){
var date = new Date();
var hour = date.getHours();
// 3. 判断小时数 改变文字信息
if(hour<12){
str = '上午好,喝杯豆浆吧';
}else if(hour<18){
str = '中午好,吃个黄焖鸡米饭';
}else{
str = '晚上好,去吃个烧烤喝个啤酒';
}
greetTxt.innerText = str;
}
</script>
效果:
2.表单密码显示隐藏效果
css部分
.password{
width: 260px;
height: 30px;
position: relative;
}
.password div{
width: 20px;
height: 20px;
position: absolute;
right: 5px;
top:5px;
background: url(icon_1.png);
}
.password div.hover{
background: url(icon_2.png);
}
.password input{
width: 100%;
height: 30px;
line-height: 30px;
}
<div class="password">
<div></div>
<input type="password" name="" id="password" value="" />
</div>
<script type="text/javascript">
// 获取元素
var eyeObj = document.querySelector('.password div');
// console.log(eyeObj);
//开关法
// 2. 注册事件 处理程序
var flag = false;
eyeObj.onclick = function() {
var passwordObj = document.getElementById('password');
flag = !flag;
if (flag) {
//修改表单的类型为文件域
passwordObj.type = 'text';
//给div标签添加hover属性
eyeObj.className = 'hover';
} else {
passwordObj.type = 'password';
eyeObj.className = '';
}
}
效果:
3.文本框焦点事件
文本框点击时,里面的默认文字隐藏,鼠标离开文本时显示文字。
onfocus 获得鼠标焦点触发
onblur 失去鼠标焦点触发
<input type="text" name="" id="txtinput" value="请输入文字" onfocus="showTxt()" onblur="hideTxt()"/>
<script type="text/javascript">
var txtinput = document.getElementById('txtinput');
function showTxt(){
if(txtinput.value=='请输入文字'){
txtinput.value = '';
}
}
function hideTxt(){
if(txtinput.value==''){
txtinput.value = '请输入文字';
}
}
</script>
</body>
效果:
4.关闭广告
css部分
*{
padding:0px;
margin: 0px;
}
a{
text-decoration: none;
}
.ads{
width: 150px;
position:fixed;
top: 100px;
left: 0px;
}
.ads a{
display: block;
width: 100%;
line-height: 35px;
background:green;
color: #fff;
text-align: center;
}
.ads div{
width: 100%;
height: 200px;
background-color: red;
color: #fff;
text-align: center;
font-size: 26px;
padding-top: 100px;
}
.ads a.hover{
width: 30px;
padding: 20px 0px;
height: auto;
line-height: 30px;
background-color: red;
}*{
padding:0px;
margin: 0px;
}
a{
text-decoration: none;
}
.ads{
width: 150px;
position:fixed;
top: 100px;
left: 0px;
}
.ads a{
display: block;
width: 100%;
line-height: 35px;
background:green;
color: #fff;
text-align: center;
}
.ads div{
width: 100%;
height: 200px;
background-color: red;
color: #fff;
text-align: center;
font-size: 26px;
padding-top: 100px;
}
.ads a.hover{
width: 30px;
padding: 20px 0px;
height: auto;
line-height: 30px;
background-color: red;
}
<div class="ads">
<!-- javascript:;解决a标签连接自动刷新页面的问题 -->
<a href="javascript:;">关闭广告</a>
<div>我是广告</div>
</div>
<script type="text/javascript">
var closeObj = document.querySelector('.ads a');
var adscon = document.querySelector('.ads div');
var flag =true;
closeObj.onclick = function(){
flag =!flag;//false
if(flag){
adscon.style.display = 'block';
this.className='';
this.innerText = '关闭广告';
document.body.style.background='#fff';
}else{
adscon.style.display = 'none';
this.className='hover';
this.innerText = '展示广告';
document.body.style.background='#000';
}
}
效果:
边栏推荐
猜你喜欢
Big coffee gathering | nextarch foundation cloud development meetup is coming
SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
$refs:组件中获取元素对象或者子组件实例:
Graduation design game mall
. Net 5 fluentftp connection FTP failure problem: this operation is only allowed using a successfully authenticated context
【NOI模拟赛】区域划分(结论,构造)
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
Four goals for the construction of intelligent safety risk management and control platform for hazardous chemical enterprises in Chemical Industry Park
SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
随机推荐
剑指offer-高质量的代码
LC 面试题 02.07. 链表相交 & LC142. 环形链表II
RuntimeError: CUDA error: CUBLAS_ STATUS_ ALLOC_ Failed when calling `cublascreate (handle) `problem solving
Basic introduction of JWT
IP address
Test of transform parameters of impdp
MYSQL----导入导出&视图&索引&执行计划
. Net core accesses uncommon static file types (MIME types)
LC interview question 02.07 Linked list intersection & lc142 Circular linked list II
服装门店如何盈利?
从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
How can brand e-commerce grow against the trend? See the future here!
MySQL user permissions
2022/07/04学习记录
异步组件和Suspense(真实开发中)
Use of completable future
Master-slave replication principle of MySQL
一条慢SQL拖死整个系统
MySql用户权限
MYSQL binlog相关命令