【问题标题】:How to get the URL of the image using jquery in wordpress?如何在wordpress中使用jquery获取图像的URL?
【发布时间】:2013-05-23 10:28:35
【问题描述】:

我正在开发一个小型灯箱 wordpress 插件,如果用户单击图像,图像应该出现在灯箱中。我如何识别用户单击的图像的 URL,因此它对于在灯箱中显示图像很有用。这可以在jquery中完成吗?任何人都可以帮忙吗?

【问题讨论】:

    标签: php jquery jquery-plugins wordpress


    【解决方案1】:

    你可以这样做

    $("img").click(function() {
        var img_path = $(this).attr('src');
    })
    

    编辑

    改变

    data: dataString,
    

    data: 'img_path='+img_path,
    

    你就可以像这样访问lightbox.php中的图片路径了

    $image_path = $_POST['image_path'];
    

    【讨论】:

    • 有没有办法将这个 img_path 值传递给 php 变量?
    • @raj: 使用ajax 怎么样?
    • $("img").click(function() { var img_path = $(this).attr('src'); $.ajax({ type:'POST', url: 'lightbox.php', dataType: 'HTML', data: dataString, success: function(data){ } }); });this 是我尝试过的ajax,但无法得到它
    • 是的,我试过但没有用。 success: function(data){ } 是否有任何更改
    • 是否可以将此img_path 附加到 img 标记中,例如
    【解决方案2】:

    获取图片网址很容易。

    $(function() {
    
      $('img').on('click', function( event ) {
        var source = this.src;    
        console.log( source );
      });
    
    });
    

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 2012-06-13
      • 2016-11-04
      • 2011-04-22
      • 2015-12-26
      相关资源
      最近更新 更多