我想创建与apple.com相同的交互式搜索栏(我知道还有很多其他帖子提出类似的问题),并且我试图使用JS切换搜索栏。搜索弹出图标:像Apple.com Angular JS
我的方法是使用jQuery:
// inserts a new search_bar
$(".glyphicon.glyphicon-search").on("click", function(){
var search_bar = document.createElement("input");
search_bar.setAttribute("type", "text");
search_bar.setAttribute("class", "form-control search left rounded");
search_bar.setAttribute("id", "search-bar");
search_bar.setAttribute("placeholder", "search");
search_bar.setAttribute("ng-model", "search.name");
$(this).parent().replaceWith(search_bar);
});
HTML:
<span class="glyphicon glyphicon-search" style="color:white;"></span>
此功能替换后点击搜索(输入标签)栏搜索glyphicon。但是我需要一种很好的方法将搜索栏捕捉到搜索图标上,点击其他地方。
任何想法?
小提琴:http://jsfiddle.net/w9pwh7fr/1/
更新:
使用$( 'HTML')上( “点击”,函数(){})似乎为关闭点击事件工作。停止传播以防止点击图形来触发这两个事件。
现在轻微的问题与搜索只能切换一次。第一次执行时。
更新小提琴: http://jsfiddle.net/w9pwh7fr/3/
'$('body')。click(function(){});' – starvator 2014-10-20 18:33:55
更新的小提琴,检查出来。但问题是关闭点击只能使用一次。如果再次重复,该元素将无法正确切换。小提琴:http://jsfiddle.net/w9pwh7fr/3/ – Ninja 2014-10-20 18:36:32