javascript - 输入键问题

标签 javascript html css mobile

我有这个搜索按钮,可以在屏幕宽度小于 1080 像素的情况下打开顶部栏,但我在移动设备上遇到问题...触摸移动设备上的按钮,它没有出现在此处的栏你有一个例子 http://cosmoscreativeagency.com/alanic/index.html

这是功能:

  • 在屏幕上> 1080px:点击搜索按钮出现白线
  • 在屏幕 <= 1080px 上:点击搜索按钮出现白色顶部搜索栏,白线设置为显示:无

下面的片段:

toggle();
window.onresize = function() {
    toggle();
}
function toggle() {
	var searchTop = document.getElementById('demo-1');	
	if (window.innerWidth <= 1080) {
		
		document.getElementById('demo-2').addEventListener('click', function(evt) {
		    
		    document.getElementById('demo-1').style.top = '0px';
		}, true);

		document.getElementById('demo-3').addEventListener('blur', function(evt) {
            document.getElementById('demo-1').style.top = '-60px';
		}, true);

	} else if (window.innerWidth > 1080) {
		document.getElementById('demo-1').style.display = 'none';
	}

}
body {
background-color: black;
}
#demo-1 {
  position: absolute;
  width: 100%;
  height: 60px;
  z-index: 300;
  transition: all 0.5s ease;
  top: -60px; }
#demo-1 input[type="search"] {
  background-color: #ffffff;
  padding-left: 50px;
  padding-right: 40px;
  width: 100%;
  height: 60px;
  color: #000000;
  border: none;
  opacity: 0.9;}
.subheader {
  position: absolute;
  top: 120px;
  width: 100%; }
#demo-2 {
  position: relative;
  display: inline-block;
  padding-right: 50px;
  float: right;
  z-index: 300; }
#demo-2 input[type="search"] {
  background: url(http://localhost/alanic/images/searchbutton.png) no-repeat 9px center;
  padding-right: 20px;
  padding-bottom: 5px;
  padding-left: 10px;
  -webkit-appearance: textfield;
  -webkit-box-sizing: content-box;
  width: 20px;
  cursor: pointer;
  border: none;
  transition: all 0.5s; }
#demo-2 input[type="search"]:hover {
  background-color: none; }
#demo-2 input[type="search"]:focus {
  width: 200px;
  padding-left: 40px;
  background-color: none;
  border-bottom: 1px solid #fff;
  color: #fff;
  cursor: auto; }
@media screen and (max-width: 1080px) {
 #demo-2 input[type="search"]:focus {
    display: none; } }
 #demo-2 input:-moz-placeholder {
  color: transparent; }
 #demo-2 input::-webkit-input-placeholder {
  color: transparent; }
 #demo-2 input:focus::-webkit-input-placeholder {
  color: #fff; }
<form id="demo-1">
  <input id="demo-3" type="search" name="buscar" placeholder="Search">
</form>			
<div class="subheader">
  <form id="demo-2">
    <input type="search" name="search" placeholder="Search">
  </form>
</div>

最佳答案

实际上,当您点击输入按钮时,它会返回空白的target.id,因为它没有id,但是当您单击表单(就在按钮附近)它将显示正确的 ID。 所以你可以做的是使用 currentTarget.idthis.id

查看targetcurrentTarget 的区别:What is the exact difference between currentTarget property and target property in javascript

在整页中检查以下代码段。

toggle();
window.onresize = function() {
    toggle();
}
function toggle() {
	var searchTop = document.getElementById('demo-1');	
	if (window.innerWidth <= 1080) {
		
		document.getElementById('demo-2').addEventListener('click', function(evt) {
		  var target = evt.target;
          console.log("target = "+evt.target.id)
          console.log("currentTarget ="+evt.currentTarget.id)
		  if (this.id === 'demo-2') {
		    document.getElementById('demo-1').style.display = 'inherit';
		  }
		}, true);

	} else if (window.innerWidth > 1080) {
		document.getElementById('demo-1').style.display = 'none';
	}

}
<form id="demo-1">
	<input type="search" name="buscar" placeholder="Search">
</form>
<form id="demo-2">
  <input type="submit" name="search" placeholder="Search">
</form>

关于javascript - 输入键问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41558976/

相关文章:

javascript - 如何更新 Leaflet 弹出窗口中的内容?

javascript - 下拉更改时 Angular js文本框值为空

javascript - 具有不同高度的 Bootstrap 布局行

javascript - jQuery 日期选择器显示在页面底部 (Keith-Wood datepicker)

html - 溢出-y和位置: absolute cause the offset when scroll bar is not at the top

javascript - 如果 key 与 lodash、vanilla js 或 d3 相等,则合并两个 javascript 对象

根据所选选项更改 div 的 JavaScript 函数 - 并不总是按预期工作?

javascript - 如何仅在页面完全呈现后才显示 div

css - 具有最小高度的 div 和子 div 具有父级的高度

html - 仅拉伸(stretch)图像的一部分