【问题标题】:Can't figure out how to get the src (full path) of a specific img with the class name cboxPhoto无法弄清楚如何使用类名 cboxPhoto 获取特定 img 的 src(完整路径)
【发布时间】:2015-04-19 17:29:00
【问题描述】:

我是 PHP 和 JavaScript/jQuery 的新手。

我正在寻找在灯箱(颜色箱)类型“窗口”中显示的图像的 src 变量,以便我可以将该变量传递给其他变量,以便能够通过社交分享照片媒体。这是我现在无法使用的代码:

var imgSrc   = $('.cboxPhoto').attr('src');
var twitter  = "<a rel='nofollow' href='https://twitter.com/intent/tweet?url='+imgSrc+'&text=Check out JG Pet Photography&hashtags=dogs,photos'>&#xf099;</a>";
var facebook = "<a rel='nofollow' href='https://www.facebook.com/sharer/sharer.php?u='+imgSrc+'>&#xf230;</a>";
var google   = "<a rel='nofollow' href='https://plus.google.com/share?url='+imgSrc+'>&#xf0d5;</a>";

jQuery(document).ready(function() {  
    jQuery('#cboxContent').append(
    '<div id="cboxSocial" class="icon-soc">' + imgSrc + twitter + facebook + google + shop + '</div><div class="clear"></div>'); 
});

类 cboxPhoto 仅在灯箱激活时显示。不确定这是否重要。您可以在 www.jeffreygelt.photography 看到它工作/不工作。只需向下滚动并点击其中一张照片。

【问题讨论】:

  • 点击事件后需要写attr()。在页面加载时编写它会给你undefined,因为稍后会生成类元素。
  • 使用 prop 而不是 attr,因为 attr 给出静态值,而 prop 给出动态值。也把它放在 document.ready

标签: javascript php jquery html


【解决方案1】:

这里有几个问题:

  1. 问题在于您的 $('.cboxPhoto').attr('src'); 调用的时间...它在文档准备好让选择器找到节点之前运行。

  2. 还有一个问题:您在创建twittergoogle 等时误用了引号。

将所有内容移到您的 onReady 函数中,然后将单引号更改为双引号十,包括变量,它应该可以工作:

jQuery(document).ready(function() {
        var imgSrc   = $('.cboxPhoto').attr('src');
        var twitter  = "<a rel='nofollow' href='https://twitter.com/intent/tweet?url="+imgSrc+"&text=Check out JG Pet Photography&hashtags=dogs,photos'>&#xf099;</a>";
        var facebook = "<a rel='nofollow' href='https://www.facebook.com/sharer/sharer.php?u="+imgSrc+">&#xf230;</a>";
        var google   = "<a rel='nofollow' href='https://plus.google.com/share?url="+imgSrc+">&#xf0d5;</a>";
        jQuery('#cboxContent').append(
        '<div id="cboxSocial" class="icon-soc">' + imgSrc + twitter + facebook + google + shop + '</div><div class="clear"></div>'); 
    });

如果您想在点击 $('.cboxPhoto').attr('src'); 时实现这一点,那么您需要分配 onclick-handler:

jQuery(document).ready(function() {
     $('.cboxPhoto').click(function(){
        var imgSrc   = $(this).attr('src');
        var twitter  = "<a rel='nofollow' href='https://twitter.com/intent/tweet?url="+imgSrc+"&text=Check out JG Pet Photography&hashtags=dogs,photos'>&#xf099;</a>";
        var facebook = "<a rel='nofollow' href='https://www.facebook.com/sharer/sharer.php?u="+imgSrc+">&#xf230;</a>";
        var google   = "<a rel='nofollow' href='https://plus.google.com/share?url="+imgSrc+">&#xf0d5;</a>";
        jQuery('#cboxContent').append(
        '<div id="cboxSocial" class="icon-soc">' + imgSrc + twitter + facebook + google + shop + '</div><div class="clear"></div>'); 
      });
    });

最后,如果有问题的图像在页面加载后动态插入到 DOM 中,您可能需要执行以下操作:

$(document).ready(function(){
   $(document.body).on('click', '.cboxPhoto' ,function(){
    var imgSrc   = $(this).attr('src');
            var twitter  = "<a rel='nofollow' href='https://twitter.com/intent/tweet?url="+imgSrc+"&text=Check out JG Pet Photography&hashtags=dogs,photos'>&#xf099;</a>";
            var facebook = "<a rel='nofollow' href='https://www.facebook.com/sharer/sharer.php?u="+imgSrc+">&#xf230;</a>";
            var google   = "<a rel='nofollow' href='https://plus.google.com/share?url="+imgSrc+">&#xf0d5;</a>";
            jQuery('#cboxContent').append(
            '<div id="cboxSocial" class="icon-soc">' + imgSrc + twitter + facebook + google + shop + '</div><div class="clear"></div>'); 

   });
})

【讨论】:

  • 感谢您的建议,但仍然无法正常工作。我仍然“未定义”。 @vinayakj,我也试过你的建议,但也没有用。
  • 除了 document.ready 之外还有其他东西,比如 onclick 之类的东西吗?由于 cboxPhoto 类的图像在灯箱打开之前不存在。
  • 好的,我刚刚更新了答案(有几处更改)......但是如果页面加载时图像不存在,那么它也不起作用。您是否有指向存在此问题的网站/页面的链接?
  • 感谢您的努力,但这也不起作用。我尝试用您实际单击的元素替换 .cboxPhoto 以启动灯箱,但这也不起作用。我想我会尝试联系 Colorbox 的开发人员,看看他/她能为我做些什么。再次感谢。
  • 听起来不错...还有一件事要尝试...查看最后一段代码..这会将事件附加到动态插入的节点。如果您知道要将图像添加到的父节点,则应使用 $([selector for parent node]).on('click', '.cboxPhoto' ,function(){... 但这应该可以工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
相关资源
最近更新 更多