JQuery下拉菜单

心不动则不痛 提交于 2019-11-27 10:06:44
<!DOCTYPE html>
<html>
<head>
	<title>下拉</title>
	<style type="text/css">
		#input{
			width: 100px;
			height: 20px;
			position: absolute;
			top: 10px;
			left: 100px;
			border: 2px solid #ccc;
		}
		#choose{
			width: 100px;
			height: auto;
			position: absolute;
			top: 18px;
			left: 60px;
			display: none;
		}
		#choose li{
			width: 100px;
			height: 20px;
			line-height: 20px;
			text-align: center;
			list-style: none;
			border: 1px solid #ccc;
			border-top: none;
		}
	</style>
</head>
<body>
<input id="input" type="text" name="">
<ul id="choose">
	<li>1</li>
	<li>10</li>
	<li>100</li>

</ul>
<script  src="https://code.jquery.com/jquery-git.js"></script>

<script>
   $('#input').click(function(){
    $('#choose').show();
    $('#choose li').click(function(){
          
    	var value = $(this)[0].innerHTML;
    	$('#input').val(value);
    	   $('#choose').hide();
    });
});
 </script>

</body>
</html>

  

<!DOCTYPE html>
<html>
<head>
	<title>下拉</title>
	<style type="text/css">
		#input{
			width: 100px;
			height: 20px;
			position: absolute;
			top: 10px;
			left: 100px;
			border: 2px solid #ccc;
		}
		#choose{
			width: 100px;
			height: auto;
			position: absolute;
			top: 18px;
			left: 60px;
			display: none;
		}
		#choose li{
			width: 100px;
			height: 20px;
			line-height: 20px;
			text-align: center;
			list-style: none;
			border: 1px solid #ccc;
			border-top: none;
		}
	</style>
</head>
<body>
<input id="input" type="text" name="">

<ul id="choose">
	<li>1</li>
	<li>10</li>
	<li>100</li>
	
</ul>


<script>
   
 
  var  objInput = document.getElementById("input");

    objInput.addEventListener("click",function () {
        console.log("click");
       var obj=document.getElementById("choose");

        obj.style.display="block";
       var inputs=obj.getElementsByTagName('li');
       console.log(inputs);
       for (var i = 0; i <inputs.length; i++) {
       	  inputs[i].addEventListener("click",function(i){
       	  	console.log(i);
       	  	objInput.value=i.path[0].innerHTML;
       	  	obj.style.display="none";

       	  });
       }


 });
 </script>

</body>
</html>

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!