【问题标题】:How to get datalist's image src or id using javascript?如何使用 javascript 获取 datalist 的图像 src 或 id?
【发布时间】:2020-08-30 14:53:31
【问题描述】:

我尝试使用 javascript 在数据列表中获取图像的 src 或 id。 以下是部分代码:

<script type="text/javascript">
          $(document).ready(function () {
              $('.productImage').mouseover(function () {
                  var imageUrl = $('.productImage').attr('src');
                  //alert(imageUrl);
                  $.ajax({
                     type: "POST",
                     url: "WebServiceVar.asmx/getVarImage",
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     data: "{'imageUrl':'" + imageUrl + "'}",
                     success: function (data) {
                     }
                  })
              });
          });
       </script>

当鼠标悬停在 datalist 中的任何图像上时,alert(imageUrl) 方法仅显示“images/products/image1.png”,尽管 DataList 中有多个图像。

【问题讨论】:

    标签: javascript asp.net image src datalist


    【解决方案1】:

    您需要添加以下代码,这对您有帮助。

     var imageUrl = $(this).attr('src');
    

    如果您的鼠标悬停事件调用任何图像悬停,则当前图像 src 您使用代码行上方的“this”获得。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      相关资源
      最近更新 更多