0
        <!DOCTYPE HTML>
        <html>
          <head>
            <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
            <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

            <style>
              body {
                margin: 0px;
                padding: 0px;
              }
              #myCanvas
              {
                background-color: blue;
              }
              .garagedoorthumbnail:hover
              {
                border: 1px solid green;
              }
            </style>
          </head>
          <body>
            <canvas id="myCanvas" width="540" height="305"></canvas>
            <script>
            var canvas = document.getElementById('myCanvas');
            var context = canvas.getContext('2d');
            var imageObj = new Image();

            imageObj.onload = function() {
                context.drawImage(imageObj, 0, 0);
            };
            imageObj.src = 'backgroundgaragedoor.png';

            $( ".garagedoorthumbnail" ).click(function() {
                console.log("hello");
                alert( "Handler for .click() called." );
            });
            </script>
            <div id="doorgallary">
                <p class="garagedoorthumbnail" src="garagedoor-beadedpanel.png">aaa</p>
                <img class="garagedoorthumbnail"  src="garagedoor-beadedpanelclassic.png" />
                <img class="garagedoorthumbnail"  src="garagedoor-beadedpaneltrifold.png" />
                <img class="garagedoorthumbnail"  src="garagedoor-beadedpaneltrifoldstockton.png" />
            </div>
          </body>
        </html>

click 函数根本没有触发。我将一个更改为一个段落,看看它是否可能是元素。我已经多次使用 click 但这次它没有触发,我还没有看到有什么问题......

4

1 回答 1

6

您正在尝试在元素存在之前绑定处理程序。将您的代码包装在 DOM 就绪处理程序中:

$(document).ready(function() {
    $(".garagedoorthumbnail").click(function() {
        console.log("hello");
        alert("Handler for .click() called.");
    });
});
于 2013-10-10T20:38:14.643 回答