jQuery点击图片什么也没做

时间:2013-09-15 22:03:12

标签: javascript jquery

我试图在单击图像时获取一些jQuery代码,我的问题是它只会在我单击图像时执行。我认为我的问题是我将所有图像放在一个名为photo的div中,其宽度和高度都是固定的。图像被约束为适合div内部而不会使图像偏离其原始约束。图像不会填充div,所以当我单击图像外部时,我点击div而不是图像。我如何能够在下面更改我的代码,以便仅在单击photo div中的图像时执行?我是jQuery的新手,所以请原谅我的问题,如果这是一个相当简单的修复。

$("body").click(function(event) {
    var $target = $(event.target).attr('class');
    var $idOfPhoto = event.target.id;
    if ($target == "photo") {
$('#lightBox').lightbox_me({
    centered:true,
    onLoad:function() {
        $('#lightBox').empty();
        $('#lightBox').prepend('<img src="/Victoria/images/photoGallery/' + $idOfPhoto + '" />');
    }
});

}
});

以下是完整的代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Victoria Mendiola</title>
        <link rel="stylesheet" href="css/style.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <link type="text/css" href="css/jquery.jscrollpane.css" rel="stylesheet" media="all" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript" src="css/jquery.mousewheel.js"></script>
        <script type="text/javascript" src="css/jquery.jscrollpane.min.js"></script>
        <script src="/resources/library/jquery.lightbox_me.js"></script>
        <script src="/resources/library/jquery.browser.js"></script>
        <script src="jquery.cycle.all.js"></script>
    </head>

<body>
    <div id="lightBox">

    </div>
    <div class="container">
        <div class="logo">
        <img class="victoriaLogo" src="images/Victoria-logo.png" />
        </div>
        <div class="navigation">
            <a class="navText" href="index.php">Home</a>
            <a class="navText" href="gallery.php">Gallery</a>
            <a class="navText" href="#about">About</a>
            <a class="navText" href="contact.php">Contact</a>
        </div>
    <script>
        $(function()
{
    $('.galleryContainer').jScrollPane();
});
    </script>
    <div class="galleryContainer">
<?php
require 'DB.php';

    try{      
    $stmt ='SELECT * FROM victoria';
    foreach ($conn->query($stmt) as $row)
        {
        echo ('<div class="photo" id="' . $row['name'] . '"> 
        <img src="images/photoGallery/thumbnails/' . $row['name'] . '" /> </div> </a>');

        }
    }  catch (PDOException $e){
        echo 'Connection failed: ' . $e->getMessage();
    }


?>
    </div>

    <script>
        $("body").click(function(event) {
            var $target = $(event.target).attr('class');
            var $idOfPhoto = event.target.id;
            if ($target == "photo") {
        $('#lightBox').lightbox_me({
            centered:true,
            onLoad:function() {
                $('#lightBox').empty();
                $('#lightBox').prepend('<img src="/Victoria/images/photoGallery/' + $idOfPhoto + '" />');
            }
        });

        }
        });
</script>
</div>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

$('.photo').click(function() {
  $('#lightBox').lightbox_me({
    centered:true,
    onLoad:function() {
      $('#lightBox').empty();
      $('#lightBox').prepend('<img src="/Victoria/images/photoGallery/' + $idOfPhoto + '" />');
    }
  });
});