[英]jquery search box and button
您不需要Jquery。 CSS樣式將為您做到這一點!
通過將內容包裝在一個元素中,可以顯示/隱藏包裝器中的任何(/所有)元素。 我在下面做了一個基本的演示:
請注意 .search
類的padding / background僅用於演示,可以對其進行編輯/刪除,並且仍保留其功能。
button { display: none; } .search:hover button { display: inline-block; } .search { background: gray; display: inline-block; padding: 10px; }
<div class="search"> <input type="text" placeholder="type here" /> <button>SEARCH</button> </div>
但是,如果您被迫使用Javascript / jquery,那么在這里我不會重復它,而是參考chipChocolate.py的答案
將hover
事件附加到#search-button
和#search-text
。
$('#search-text').hide(); $('#search-button, #search-text').hover(function() { $('#search-text').show(); }, function() { $('#search-text').hide(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="search-text" type="text" / ><input id="search-button" type="button" value="Search" />
針對您的問題的JavaScript解決方案。 檢查此jsfiddle鏈接:
$('.search').hover(function() { $('#search-text').show(); }, function() { $('#search-text').hide(); }); $('#search-text').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="search"> <button>SEARCH</button> <input type="text" placeholder="type here" id='search-text' /> </div>
刪除第二個功能。 這是hoveroverout。 使用以下內容:
$('#search-button').hover(function () {
$('#search-text').show();
});
這可能有效
$('#search-text').hide(); //on DOM Load hide the element
$('#search-button').mouseover(function()
{
$('#search-text').show(); //When mouse enters the element, then show the textbox input
});
$('#search-button').hover(function () { $('#search-text').show(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" id="search-button" value="search"/> <input type="text" id="search-text" style="display:none;"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.