0

当您单击 div(我也尝试将其设为按钮)时,我试图触发 JQuery 函数,但无济于事。Google Chrome Inspector 甚至无法识别在幕后创建的事件处理程序。

<!DOCTYPE>
<html>
    <head>
        <meta charset="UTF-8">;
        <script type = "text/javascript" src = "http://code.jquery.com/jquery-1.9.1.js"></script>
    </head>
    <body>
        <div id = "main" style = "width: 500px; height: 500px; background-color: black; color: white;">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eius aliquam nobis optio ut ratione a eligendi excepturi cumque est commodi? Sed, odit, culpa deserunt distinctio at commodi modi architecto aliquam.
        </div>
        <button class = "button" href="#" style = "width: 50px; height: 50px; background-color: black;"></button>
        <script type = "text/javacsript">
            $(document).ready(function(){
                $(".button").click(function(){
                    $("main").val("IT WORKED");
                });
            };
        </script>
</html>
4

1 回答 1

2

您忘记使用#id 选择器,也使用text()orhtml()代替 val() fordiv并且您还错过了document.ready.

现场演示

 $(document).ready(function(){
     $(".button").click(function(){
           $("#main").text("IT WORKED");
     });
  });
于 2013-04-19T18:51:27.297 回答