javascript - 退出搜索框事件时如何重置占位符?

标签 javascript html css

我有一个叠加搜索框(检查代码)。搜索框有一个占位符“Sök”,假设用户在文本框中写了一些东西然后退出(按右上角的 x)。然后我希望删除用户写的文本并重置占位符,因此只要用户再次输入搜索框,文本就会被删除并且占位符会返回。如何创建此事件? 代码:

body{
background: white;
font-family: 'Montserrat', sans-serif;
padding-bottom: -1px;
}


span{
  display: inline-block;
}


.backgroundlogo{
  margin-top:-1400px;
  z-index: -1;
  position: relative;
width: 100%;
}


.container{
  width: 80%;
  margin: 0 auto;

}
header{
  background: none;
}
* {
margin:0;
padding:0;
}
header ::after {
  content: "";
  display: table;
  clear: both;
}
nav{
float: right;
padding-right: 230px;
}
nav li{
  display: inline-block;
  padding-left: 45px;
  padding-top: 20px;
  padding-bottom: 20px;
}
nav ul{
  list-style: none;
  display: inline-block;
  padding-top: 25px;

}
nav a {
  font-size: 12px;
  color: black;
  font-weight: 600;
  text-decoration: none;
  text-align: center;
  text-transform: uppercase;
}
nav a:hover{
  color: red;
}
nav li:hover{
}

.fa-bars{
  color: black;
  font-size: 14px;
  padding-left: 15px;
}

.fa-bars:hover{
    color: red;
    cursor: pointer;
}

.wrapper{
  position: relative;
  height: 100%;
  width: 100%;
}

.backgroundlogo{

}



.bild1{
  height: 350px;
  width: 600px;
  margin-top: 100px;
  margin-left: 80px;
position: absolute;
z-index: 4;
  background-image: url('Img/KBA.jpg');
  background-position: 10% 30% ;
  background-size: 180%;

}

.bild2{
  height: 350px;
  width: 600px;
  margin-top: 140px;
  margin-left: 120px;
 z-index: 3;
  position:absolute;
  background-color: #3D6BB8;

}

.entrytext{
float: right;
margin-right: 90px;
margin-top: 175px;
clear: both;


}
.entrytext>h1{
  font-weight: 800;
  font-style: normal;
  font-size: 54px;
}

.entrytext>button{
border: none;
display: inline-block;
background-color: #38b272;
color: white;
padding: 8px 10px 8px 15px;
letter-spacing: 6px;
border-radius: 8px;
font-weight: 500;
font-size: 17px;
text-align: left;
margin-top:   20px;
box-shadow: 20px 15px black;
}

.entrytext>button:hover{
  border: none;
  display: inline-block;
  background-color: #c12147;
  color: white;
  padding: 8px 10px 8px 15px;
  letter-spacing: 6px;
  border-radius: 8px;
  font-weight: 500;
  font-size: 17px;
  text-align: left;
  margin-top: 20px;
}
button:focus {outline:0;}

.fa-angle-right{
  font-size: 20px;
  padding-left: 30px;

}


.entrytext>h2{
font-size: 14px;
font-weight: 600;
margin-top: 20px;
}
.citygalleria{
  color: #CC2244;
}


.brand{
  height: 110px;
  width: 750px;
  margin: 600px auto;
  background-color: #CFCFCF;
  clear: both;
  z-index: 11;

}




.openBtn {
  background: #f1f1f1;
  border: none;
  padding: 10px 15px;
  font-size: 20px;
  cursor: pointer;
}

.openBtn:hover {
  background: #bbb;
}

.overlay {
  height: 100%;
  width: 100%;
  display: none;
  position: fixed;

  z-index: 10;
  top: 0;
  left: 0;
  background-color: white;
  background-color: rgba(255,255,255, 0.8);
}


.overlay-content {
  position: relative;
  top: 20%;
  width: 80%;
  text-align: center;
  margin-top: 30px;
  margin: auto;
}

.overlay .closebtn {
  position: absolute;
  top: 20px;
  right: 45px;
  font-size: 60px;
  cursor: pointer;
  color: black;
}

.overlay .closebtn:hover {
  color: #ccc;
}

.overlay input[type=text] {
  padding: 15px;
  font-size: 50px;
  font-weight: bold;
  border: none;
  background:none;
  margin: 0 auto;
   text-decoration: none;
    border-bottom: 6px solid black;
border-bottom-left-radius: 5px;
  color:black;
text-align:center;
  width: 100%;


}
input::placeholder {
  color: black;
}

.overlay input[type=text]:hover {
  background: none;
}

.overlay button {
  float: left;
  width: 20%;
  padding: 15px;
  background: #ddd;
  font-size: 17px;
  border: none;
  cursor: pointer;
}

input:focus {outline:0;}

.overlay button:hover {
  background: #bbb;
}

.type1{
  width: 1700px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <link rel="stylesheet" href="style.css" type="text/css">
    <meta charset="utf-8">
    <script src="https://kit.fontawesome.com/908c2e5c96.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Montserrat:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i&display=swap" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="slick/slick.css"/>
    <title>Kungsmässan — Måste upplevas!</title>
  </head>
  <body>

<header>

<div class="container">
<nav>
  <ul>
    <li><a href="#">Butiker</a></li>
    <li><a href="#">Resturang & Café</a></li>
    <li><a href="#">Utbyggnad</a></li>
    <li><a href="#">Öppetider</a></li>





    <div id="myOverlay" class="overlay">
  <span class="closebtn" onclick="closeSearch()" title="Close Overlay">×</span>
  <div class="overlay-content">
    <form action="/action_page.php">
      <input class="type1" id="type2" onblur="this.placeholder = 'Sök'" onfocus="this.placeholder = ''" type="text" placeholder="Sök" name="search">

    </form>
  </div>
</div>

   <i onclick="openSearch()" id="openBtn" class="fas fa-search"></i>


   <script>
function openSearch() {
  document.getElementById("myOverlay").style.display = "block";
}

document.addEventListener('keydown',function(){document.getElementById('type2').focus();});



function closeSearch() {
  document.getElementById("myOverlay").style.display = "none";
}
</script>


      <i class="fas fa-bars"></i>
  </ul>
</nav>
</div>
</header>


  <div class="bild1">
  </div>
  <div class="bild2">
  </div>
  <div class="entrytext">
    <h1>Sveriges bästa <br/> <span class="citygalleria">citygalleria.</span> Mitt  <br/> i Kungsbacka.</h1>
    <h2>35 000 KVADRATMETER OCH ÖVER 100 AFFÄRER!</h2>
    <button type="LÄS MER" name="button ">LÄS MER<i class="fas fa-angle-right"></i></button>
  </div>

<div class="brand">
</div>




<span>
<img class="backgroundlogo" src="Img/bg.png" alt="">
</span>


  </body>
</html>

最佳答案

如果您在单击关闭按钮时将输入的值设置回空,则占位符应该再次出现:

const button = document.querySelector( 'button' );
const input = document.querySelector( 'input' );
button.addEventListener( 'click', event => {
  input.value = '';
});
<input type="text" placeholder="Sok">
<button>Close</button>

关于javascript - 退出搜索框事件时如何重置占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57692327/

相关文章:

javascript - 使用动态对象名称访问对象

javascript - 通过循环将 CSS 类应用于每个 <li> 元素

html - 如何修改我的 CSS 以需要一个类而不是 "+ label"?

css - IE 8.渐变背景+图片

javascript - jQuery 改变元素是否有类

java - Selenium WebDriver(Java) : JavascriptExecutor fails to click element, 而 WebElement.click() 效果很好

javascript - 如何将复选框值数据绑定(bind)到 Javascript 对象

javascript - 如何从父html通过iframe传递参数?

html - 嵌套 Bootstrap 3 网格和 Angular Directive(指令)中的全宽元素

asp.net - 如何在 ASP.Image 控件上显示 ASP.NET ImageButton