当前位置:网站首页>JS small exercise ---- time sharing reminder and greeting, form password display hidden effect, text box focus event, closing advertisement
JS small exercise ---- time sharing reminder and greeting, form password display hidden effect, text box focus event, closing advertisement
2022-07-07 07:15:00 【Luqi zz】
Catalog
1. Time sharing reminder greeting
2. The form password shows the hidden effect
1. Time sharing reminder greeting
Judge according to different time of the system , So you need to use the date built-in object
Open the page in the morning and say : Good morning , Dear users ! 0-12
Open the page in the afternoon and say : Good afternoon, , Dear users !12-18
Open the page at night and say : Good evening , Dear users ! Greater than 18
Need one div Elements , Show different greetings , Modify the element content
<div>
<span id="greetTxt"></span>
<input type="button" name="" id="greet" value=" Show greetings " />
</div> <script type="text/javascript">
// 1. Get elements
var greetTxt = document.getElementById('greetTxt');
var greet = document.getElementById('greet');
var str = '';
// 2. Get the current number of hours
greet.onclick = function(){
var date = new Date();
var hour = date.getHours();
// 3. Judge the number of hours Change the text message
if(hour<12){
str = ' Good morning , Have a cup of soymilk ';
}else if(hour<18){
str = ' Good noon , Have a stewed chicken and rice ';
}else{
str = ' Good evening , Go to a barbecue and have a beer ';
}
greetTxt.innerText = str;
}
</script>effect :


2. The form password shows the hidden effect
css part
.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">
// Get elements
var eyeObj = document.querySelector('.password div');
// console.log(eyeObj);
// Switching method
// 2. Registration events The handler
var flag = false;
eyeObj.onclick = function() {
var passwordObj = document.getElementById('password');
flag = !flag;
if (flag) {
// Change the form type to file field
passwordObj.type = 'text';
// to div add hover attribute
eyeObj.className = 'hover';
} else {
passwordObj.type = 'password';
eyeObj.className = '';
}
}effect :
3. Text box focus event
When the text box is clicked , The default text inside is hidden , Display text when the mouse leaves the text .
onfocus Get mouse focus trigger
onblur Lose mouse focus trigger
<input type="text" name="" id="txtinput" value=" Please enter text " onfocus="showTxt()" onblur="hideTxt()"/>
<script type="text/javascript">
var txtinput = document.getElementById('txtinput');
function showTxt(){
if(txtinput.value==' Please enter text '){
txtinput.value = '';
}
}
function hideTxt(){
if(txtinput.value==''){
txtinput.value = ' Please enter text ';
}
}
</script>
</body>effect :

4. Close advertising
css part
*{
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:; solve a The problem of automatically refreshing the page of tag connection -->
<a href="javascript:;"> Close advertising </a>
<div> I'm an advertisement </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 = ' Close advertising ';
document.body.style.background='#fff';
}else{
adscon.style.display = 'none';
this.className='hover';
this.innerText = ' Display advertising ';
document.body.style.background='#000';
}
}effect :

边栏推荐
- Matlab tips (30) nonlinear fitting lsqcurefit
- Algorithm --- bit count (kotlin)
- MySQL SQL的完整处理流程
- SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
- Learning records on July 4, 2022
- Network foundation - header, encapsulation and unpacking
- Apache AB stress test
- Procedure in PostgreSQL supports transaction syntax (instance & Analysis)
- . Net core accesses uncommon static file types (MIME types)
- Paranoid unqualified company
猜你喜欢

How DHCP router works

. Net core accesses uncommon static file types (MIME types)

Circulating tumor cells - here comes abnova's solution

SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)

Basic introduction of JWT

mips uclibc 交叉编译ffmpeg,支持 G711A 编解码

How does an enterprise manage data? Share the experience summary of four aspects of data governance

Answer to the second stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition

$parent(获取父组件) 和 $root(获取根组件)

Prime partner of Huawei machine test questions
随机推荐
Kuboard无法发送邮件和钉钉告警问题解决
A slow SQL drags the whole system down
Please tell me how to monitor multiple schemas and tables by listening to PgSQL
How DHCP router works
Lvs+kept (DR mode) learning notes
Special behavior of main function in import statement
Bus message bus
大咖云集|NextArch基金会云开发Meetup来啦
.net 5 FluentFTP连接FTP失败问题:This operation is only allowed using a successfully authenticated context
2018 Jiangsu Vocational College skills competition vocational group "information security management and evaluation" competition assignment
How to share the same storage among multiple kubernetes clusters
leetcode 509. Fibonacci number
from . onnxruntime_ pybind11_ State Import * noqa ddddocr operation error
$refs: get the element object or sub component instance in the component:
Please answer the questions about database data transfer
$parent (get parent component) and $root (get root component)
Tumor immunotherapy research prosci Lag3 antibody solution
Freeswitch dials extension number source code tracking
Answer to the second stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
Exception of DB2 getting table information: caused by: com ibm. db2.jcc. am. SqlException: [jcc][t4][1065][12306][4.25.13]
