【问题标题】:Close JQuery Image with click outside div在 div 外部单击关闭 JQuery 图像
【发布时间】:2012-10-16 04:09:21
【问题描述】:

我希望能够以 3 种方式关闭隐藏显示的弹出式画廊:点击我已经拥有的图像。当我单击外部框时关闭并在选择另一个菜单链接时关闭,即单击主页并弹出重置关闭。

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Website</title>

<link rel="stylesheet" type="text/css" href="../style-projects-jquery.css" /> 

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.lightbox-0.5.js"></script>

<script type="text/javascrip" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="js/jquery.localscroll-min.js" type="text/javascript"></script>
<script src="js/jquery.scrollTo-min.js" type="text/javascript"></script>


    <link rel="stylesheet" type="text/css" href="css/jquery.lightbox-0.5.css" media="screen" />

        <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />

        <script type="text/javascript">
        $(document).ready(function () {
            $.localScroll.defaults.axis = 'x';
            $.localScroll();
        });
        </script>  

     <script type="text/javascript">
    $(function() {
        $("#gallery a").lightBox();
    });
    </script>   

<script type="text/javascript">
$(".gallery").click(function(e){
    e.stopPropagation();
});

 </script>  

<script type="text/javascript">
$(".menu a").click(function(e){
    e.stopPropagation();
});

 </script>  

</head>
<body>

                <a name="home"></a>
    <div id="menu">
        <ul>
            <li><a href="#home">Home</a></li>
            <li><a href="#box1">Box 1</a></li>
            <li><a href="#box2">Box 2</a></li>
            <li><a href="#box3">Box 3</a></li>
            <li><a href="#box4">Box 4</a></li>
            <li><a href="#box5">Box 5</a></li>
            <li><a href="#box6">Box 6</a></li>
            <li><a href="#box7">Box 7</a></li>
            <li><a href="#box8">Box 8</a></li>
        </ul>
    </div>
<div id="box6" class="box">
            <h2><a name="box6">Sixth Box</a></h2>
            <p><div id="gallery">

        <li>
            <a href="photos/image1.jpg" title="Image 1 $('#gallery').lightBox();">
                <img src="photos/thumb_image1.jpg" width="72" height="72" alt="" />
            </a>
        </li>
        <li>
            <a href="photos/image2.jpg" title="Image 2 $('#gallery a').lightBox();">
                <img src="photos/thumb_image2.jpg" width="72" height="72" alt="" />
            </a>
        </li>
        <li>
            <a href="photos/image3.jpg" title="Image 3 $('#gallery a').lightBox();">
                <img src="photos/thumb_image3.jpg" width="72" height="72" alt="" />
            </a>
        </li>
        <li>
            <a href="photos/image4.jpg" title="Image 4 $('#gallery a').lightBox();">
                <img src="photos/thumb_image4.jpg" width="72" height="72" alt="" />
            </a>
        </li>
        <li>
            <a href="photos/image5.jpg" title="Image 5 $('#gallery a').lightBox();">
                <img src="photos/thumb_image5.jpg" width="72" height="72" alt="" />
            </a>
        </li>

</div></p>
        </div>

【问题讨论】:

    标签: jquery html onclick


    【解决方案1】:
    $(document).click(function(e){
         var elem = $(e.target).attr('class');
         if (elem != 'slidingDiv') {  ////slidingDiv is a class name where do not want to close when click on it...
             ///Your code to close popup.....
         }
    });
    

    【讨论】:

      【解决方案2】:

      尝试为每个元素创建单独的 id 并将它们标记在隐藏块中,如下所示。

      $(document).click(function (e) {
          if ((e.target.id != "a_IDName") && (e.target.id != "imgIDName"){ 
               // if click is not in 'mydiv'
               $("#a_IDName").hide("fast");
          }
         //More if's for each Image Condition.
      });
      

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        问题可能是调用插件的jquery选择器好像不对。 Ty 更改为 $('a#gallery').lightBox(); 并在您想要公开该行为的链接中添加一个 id 'gallery'。我检查了site,看来您不需要任何其他脚本即可通过单击外部来隐藏图像。

        【讨论】:

          【解决方案4】:

          你需要为此处理文档点击事件

          $(document).click(function (e) {
                  //apply your code here to check which element is clicked by using $(e.target)
          });
          

          $(e.target) 将为您提供触发点击事件的对象,之后您可以按类或 id 进行检查

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多