如何从单击的 img 中获取 src 值

How get the src value from the img clicked

我会得到点击 img 的 src 值。我有这个 HTML https://jsfiddle.net/4k541boj/code

 <span id="sinistra">
        <img src="http://www.bicielettriche.bikeitalia.it/wp-content/uploads/2014/11/bici-elettrica-piaggio.png">
        <img src="https://eradellabicicletta.files.wordpress.com/2013/01/bici-da-corsa-rb1000-team-edition.jpg">
        <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR9ty91M-QHPrgy6woAJAgdF68UrTD8c91WXz5dfznU1R_2GQ5Qjw">
        </span>

$('#sinistra img').click(function(){
   alert($(this).attr('src'));
});

但我不明白我哪里错了。 有人可以帮我吗?

<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script>
      $(document).ready(function () {
        // Listen for clicks to "img" elements inside the element where id="sinistra"
        $("#sinistra img").click(function () {
          // Displays the source of the image clicked
          alert($(this).attr("src"));
        });
      });
    </script>
  </head>
  <body>
    <span id="sinistra">
      <img
        src="http://www.bicielettriche.bikeitalia.it/wp-content/uploads/2014/11/bici-elettrica-piaggio.png"
      />
      <img
        src="https://eradellabicicletta.files.wordpress.com/2013/01/bici-da-corsa-rb1000-team-edition.jpg"
      />
      <img
        src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR9ty91M-QHPrgy6woAJAgdF68UrTD8c91WXz5dfznU1R_2GQ5Qjw"
      />
    </span>
  </body>
</html>

Jquery 是否包含在您的 page.From 提琴手 link 中,您提供的很明显 Jquery 未包含在 'uncaught ReferenceError: $ is not defined' 错误中来了

你的 jsFiddle 工作正常,但你必须添加 jQuery 库,用齿轮点击单词 javascript 并在 "Frameworks & extensions" 输入中选择 jQuery .

但是你的代码是正确的。