获取图像标题并附加到当前URL的末尾以获取链接

时间:2016-08-29 14:32:25

标签: javascript jquery

我在PHP中使用动态内容,我需要获取图片标题,然后将其附加到当前网址的末尾。当用户点击图像时,他们将被发送到该目的地。

例如 - www.foo.com/bar/是当前的网址。图像标题是动态的"当前图像标题"。我需要锚类和#34; item-link"现在使用" www.foo.com/bar/current-image-title"当用户点击图片时

    <a class="item-link" href="new-category">
     <img id="item-desc" src="image.jpg" title="dynamic-title">
    </a>

    <script type='text/javascript'>
     var link = $('#item-desc').each(function() {
      $(this).attr('title');

      $('a.item-link').click(function(event) {
       event.preventDefault();
      document.location.href = document.location + '/' + '$link';
    });
    </script>

我可以弄清楚如何获取和使用图片标题或如何附加到网址的末尾,但我似乎无法弄清楚如何做到这两点。

1 个答案:

答案 0 :(得分:0)

你可以尝试

$(".item-link").each(function () {
    var $this = $(this), $img = $this.find("img");
  $this.attr("href", $this.attr("href") + "/" + $img.attr("title"));
});

https://jsfiddle.net/gkk9ohca/

上试用
相关问题