为搜索的父级提供相同的 z-index
虽然由于您没有包含示例,因此很难知道 html 的外观。如果您希望它出现在同一级别,那么您可以从门给它相同的 z-index。如果搜索部分所在的块在导航之后,那么它们自然会在相同的z-index上彼此重叠。
#nav,#search{position:absolute;z-index:9999}
例如,将#search 放在#nav 之上,因为#search 在html 中位于#nav 之后;喜欢:
<parent><nav/><search/></parent>
如果它位于它之前,并且可能与不同的父级,那么你可以使用类似的东西:
#nav{z-index:9998} #search{z-index:9999}
如果 html 更像:
<search/><parent><nav/></parent>
但是,如果您需要它来打开/关闭 z-index,并且在 #search 上根本无法修复它,那么您已经朝着正确的方向前进了。
这样的事情会很容易:
$('#search input').on('focusin', function(){//on focus
$(this).siblings('.myIcon').css({zIndex: '9999'});
}).on('focusout', function(){//on un-focus
$(this).siblings('.myIcon').css({zIndex: '1'});
});
尽管如果您能够控制 css,那么您可以使用 z-index:9999 为 .myIcon 创建一个类,然后使用以下内容:
.on{z-index:9999}/*added css*/
$('#search input').on('focusin focusout', function(){//on focus change
$(this).siblings('.myIcon').toggleClass('on');
});
做了一个小提琴:http://jsfiddle.net/filever10/pxdz5/