【问题标题】:Getting with Jquery src from a css class从 CSS 类中获取 Jquery src
【发布时间】:2018-02-01 15:13:51
【问题描述】:

我正在使用 UberGallery,但我不知道如何从“cboxPhoto”类中获取“src”:

<div id="cboxLoadedContent" style="width: 500px; overflow: auto; height: 333px;"><img class="cboxPhoto" src="test.jpg" style="cursor: pointer; width: 500px; height: 333px; float: none;"></div>

我正在尝试,但它不起作用。

var x = $('cboxPhoto').attr('src');

有什么想法吗?

【问题讨论】:

  • 你需要使用.来选择一个类:$('.cboxPhoto')
  • 谢谢,但结果总是“未定义”,带有警报(x)
  • 您确定代码是在文档加载后执行的吗?确保脚本就在结束 &lt;/body&gt; 标记之前,或使用 ready 事件。
  • 在这里工作:jsfiddle.net/barmar/eecqtz9q/2
  • cboxPhoto 类是否有多个元素?这将选择第一个。您可能需要使用更具体的选择器来获得所需的选择器。

标签: jquery css class


【解决方案1】:

用你的 Fiddle 更新(删除 php)

  • 使用点来引用类选择器$('.cboxPhoto')
  • 确保您正在加载 jQuery。
  • 为演示添加了alert(x)

$(document).ready(function() {
  $("a").click(function() {
    //var ficken = $(this).attr('href');
    var x = $('.cboxPhoto').attr('src');
    alert(x);
  });
});
#cboxLoadedContent {
  width: 500px;
  overflow: auto;
  height: 333px;
}

.cboxPhoto {
  cursor: pointer;
  width: 500px;
  height: 333px;
  float: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="">click me</a>
<div id="cboxLoadedContent">
  <img class="cboxPhoto" src="test.jpg">
</div>

【讨论】:

    猜你喜欢
    • 2011-04-25
    • 2013-11-25
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    相关资源
    最近更新 更多