【问题标题】:Add absolute path to img in jQuery object在 jQuery 对象中添加 img 的绝对路径
【发布时间】:2017-05-22 03:29:42
【问题描述】:

我想从外部站点上的特定 div 中提取一些数据,将 URL 预先附加到 img src 链接,这些链接是相对的,以使其成为绝对值,然后将该 html 放入 div 中。忽略跨站问题...

jQuery(document).ready(function() {
  var $thePage = jQuery('<div>');
  jQuery($thePage).load("https://example.com div.myitem");
  jQuery($thePage).find('img').attr('src', function (i, src) {
    var newsrc = "https://example.com" + src;
    return newsrc;
  });
  jQuery("#testdiv").html($thePage);
});

html 显示在 div 中,但 img 链接没有更新。任何想法将不胜感激!

【问题讨论】:

  • 目前的结果和预期的结果是什么?

标签: javascript jquery html


【解决方案1】:

jQuery.load 是异步的,所以你应该在回调中处理返回的数据。

  jQuery($thePage).load("https://example.com div.myitem", function () {
    // replace stuff here
    // show output on page here
  });

【讨论】:

    猜你喜欢
    • 2011-03-30
    • 2014-02-06
    • 2015-02-09
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多