JQuery 鼠标点击其它地方隐藏层

Javascript 1612 0 2012-07-23

JQuery 鼠标点击其它地方隐藏层
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<style  type="text/css">
.div{ padding:10px; border:1px dotted red; position:absolute; display:none; cursor:pointer;}
</style>
<script src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">
    $(function(){
        $("input[id=search]").focus(function(even){                                              
         var pos=$(this).offset();
      
         $(".div").css({left:pos.left,top:pos.top+20+"px",display:"block"});
         });
        
        document.onclick=function(e){
           var e=e?e:window.event;
           var tar = e.srcElement||e.target;
           if(tar.id!="search"){
               if($(tar).attr("class")=="div"){
                   $(".div").css("display","block")
               }else{
                   $(".div").css("display","none");
               }
           }
         }
    })
</script>
</head>
<body style=" padding-left:300px;">
    <input id="search" />
    <div class="div"> 
        <a href="#" target="_blank">打开我的链接</a>
    </div>
</body>
</html>

上一篇:认识jQuery jQuery选择器 jQuery中的DOM操作

下一篇:如何使用Javascript格式化数字显示

讨论数量:0

请先登录再发表讨论。 2024-04-30

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链