javascript - 在 ajax_injected 代码块中定位 iframe src

标签 javascript jquery

我有一段代码是oembed吐出来的。然后我需要定位 iframe 的 src 属性并将其替换为 data-src

她是我目前拥有的:

embed_request = {
      url: url,
      dataType: "jsonp",
      cache: false,
      success: function (data) {
        try {          
          var embed_html = (data.html).replace('src', 'data-src');
          $( "div#item"+number).html(embed_html);
        } catch (err) {
          console.log(err);
        }
      }
    };    

$.ajax(embed_request);

问题是我上面的 replace() 在代码块 data.html 中更改了太多 src

我如何在此处通过 javascript 或 jquery 判断 var embed_html = (data.html).replace('src', 'data-src'); 仅更改它运行到的 iframe 的 src在 html 代码块中。

不幸的是,今天我当前的代码不仅更改了下面的 iframe src,还更改了 src="//platform.instagram.com/en_US/embeds.js" 的 src:

<iframe src="https://www.instagram.com/p/64567SDFFDFS/embed/captioned/?cr=1&amp;v=7" class="instagram-media instagram-media-rendered" id="instagram-embed-0"  allowtransparency="true" frameborder="0" height="849" data-instgrm-payload-id="instagram-media-payload-0" scrolling="no" style="border: 1px solid rgb(219, 219, 219);"></iframe>
<script async="" defer="" src="//platform.instagram.com/en_US/embeds.js"></script>

所以我进入了最后的吐口水 View :

<iframe data-src="https://www.instagram.com/p/64567SDFFDFS/embed/captioned/?cr=1&amp;v=7" class="instagram-media instagram-media-rendered" id="instagram-embed-0"  allowtransparency="true" frameborder="0" height="849" data-instgrm-payload-id="instagram-media-payload-0" scrolling="no" style="border: 1px solid rgb(219, 219, 219);"></iframe>
    <script async="" defer="" data-src="//platform.instagram.com/en_US/embeds.js"></script>

如何只定位 iframe 的 src ?

谢谢

编辑

这里有两个oembed吐出来的html chunk代码的例子

Instagram

<div class="item active" id="item1">
<iframe class="instagram-media instagram-media-rendered" id="instagram-embed-0" src="https://www.instagram.com/p/BPodiIYgG3K/embed/captioned/?cr=1&amp;v=7" allowtransparency="true" frameborder="0" height="776" data-instgrm-payload-id="instagram-media-payload-0" scrolling="no" style="border: 1px solid rgb(219, 219, 219); margin: 1px 1px 12px; max-width: 320px; width: calc(100% - 2px); border-radius: 4px; box-shadow: none; display: block; padding: 0px; background: rgb(255, 255, 255);"></iframe>
    <script async="" defer="" src="//platform.instagram.com/en_US/embeds.js"></script>
</div>

Facebook

<div class="item " id="item2">

          <div id="fb-root" class=" fb_reset"><div style="position: absolute; top: -10000px; height: 0px; width: 0px;"><div></div></div><div style="position: absolute; top: -10000px; height: 0px; width: 0px;"><div><iframe name="fb_xdm_frame_http" frameborder="0" allowtransparency="true" allowfullscreen="true" scrolling="no" title="Facebook Cross Domain Communication Frame" aria-hidden="true" tabindex="-1" id="fb_xdm_frame_http" src="http://staticxx.facebook.com/connect/xd_arbiter/r/0eWevUAMuoH.js?version=42#channel=f3e391184&amp;origin=http%3A%2F%2Flocalhost%3A3000" style="border: none;"></iframe><iframe name="fb_xdm_frame_https" frameborder="0" allowtransparency="true" allowfullscreen="true" scrolling="no" title="Facebook Cross Domain Communication Frame" aria-hidden="true" tabindex="-1" id="fb_xdm_frame_https" src="https://staticxx.facebook.com/connect/xd_arbiter/r/0eWevUAMuoH.js?version=42#channel=f3e391184&amp;origin=http%3A%2F%2Flocalhost%3A3000" style="border: none;"></iframe></div></div></div>
    <script>(function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/fr_FR/sdk.js#xfbml=1&version=v2.3";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script><div class="fb-post fb_iframe_widget" data-href="https://www.facebook.com/cocacolafrance/posts/1526674334016658" data-width="536" fb-xfbml-state="rendered" fb-iframe-plugin-query="app_id=&amp;container_width=0&amp;href=https%3A%2F%2Fwww.facebook.com%2Fcocacolafrance%2Fposts%2F1526674334016658&amp;locale=fr_FR&amp;sdk=joey&amp;width=536"><span style="vertical-align: bottom; width: 536px; height: 404px;"><iframe name="f2115553f8" width="536px" height="1000px" frameborder="0" allowtransparency="true" allowfullscreen="true" scrolling="no" title="fb:post Facebook Social Plugin" src="https://www.facebook.com/v2.3/plugins/post.php?app_id=&amp;channel=http%3A%2F%2Fstaticxx.facebook.com%2Fconnect%2Fxd_arbiter%2Fr%2F0eWevUAMuoH.js%3Fversion%3D42%23cb%3Dfc155ce24%26domain%3Dlocalhost%26origin%3Dhttp%253A%252F%252Flocalhost%253A3000%252Ff3e391184%26relation%3Dparent.parent&amp;container_width=0&amp;href=https%3A%2F%2Fwww.facebook.com%2Fcocacolafrance%2Fposts%2F1526674334016658&amp;locale=fr_FR&amp;sdk=joey&amp;width=536" class="" style="border: none; visibility: visible; width: 536px; height: 404px;"></iframe></span></div></div>

最佳答案

您可以先将标记字符串转换为 HTML 元素(离开 DOM),然后找到任何 iframe 元素,替换它们的属性,然后将整个元素添加到 DOM:

embed_request = {
  url: url,
  dataType: "jsonp",
  cache: false,
  success: function (data) {
    try {          
      $(data.html).filter('iframe').each(function() {
        var $this = $(this);
        $this.attr('data-src', $this.attr('src')).removeAttr('src');
      }).end().appendTo("div#item"+number);
    } catch (err) {
      console.log(err);
    }
  }
};    

$.ajax(embed_request);

这是一个模型(用函数调用等替换了 ajax 调用)以表明它正在工作:

var number = 1,
    data = {html: atob("PGlmcmFtZSBzcmM9Imh0dHBzOi8vd3d3Lmluc3RhZ3JhbS5jb20vcC82NDU2N1NERkZERlMvZW1iZWQvY2FwdGlvbmVkLz9jcj0xJmFtcDt2PTciIGNsYXNzPSJpbnN0YWdyYW0tbWVkaWEgaW5zdGFncmFtLW1lZGlhLXJlbmRlcmVkIiBpZD0iaW5zdGFncmFtLWVtYmVkLTAiICBhbGxvd3RyYW5zcGFyZW5jeT0idHJ1ZSIgZnJhbWVib3JkZXI9IjAiIGhlaWdodD0iODQ5IiBkYXRhLWluc3Rncm0tcGF5bG9hZC1pZD0iaW5zdGFncmFtLW1lZGlhLXBheWxvYWQtMCIgc2Nyb2xsaW5nPSJubyIgc3R5bGU9ImJvcmRlcjogMXB4IHNvbGlkIHJnYigyMTksIDIxOSwgMjE5KTsiPjwvaWZyYW1lPjxzY3JpcHQgYXN5bmM9IiIgZGVmZXI9IiIgc3JjPSIvL3BsYXRmb3JtLmluc3RhZ3JhbS5jb20vZW5fVVMvZW1iZWRzLmpzIj48L3NjcmlwdD4=")}; // this is the html provided by you, containing the iframe and script tag, but I had to base64 encode it, as otherwise the code snippet tool wouldn't let me insert it

(function (data) {
  try {          
    $(data.html).filter('iframe').each(function() {
      var $this = $(this);
      $this.attr('data-src', $this.attr('src')).removeAttr('src');
    }).end().appendTo("div#item"+number);
  } catch (err) {
    console.log(err.message);
  }
})(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="item1"></div>

关于javascript - 在 ajax_injected 代码块中定位 iframe src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42250087/

相关文章:

javascript - 使用 Json 发布文件

javascript - 为什么 javascript "write() function"在通过 jquery 引用元素时不起作用?

javascript - 灯箱在 IE 8 中不工作

javascript - 如何在选择中使用模板?

javascript - jQuery Mobile 是原始 jQuery 或 jQuery UI 的轻量级版本吗?

javascript - Emotion CSS-in-JS 库中的 comment/** @jsx jsx */有什么作用?

javascript - for循环中的Object.defineProperty

javascript - 在触摸滚动条中居中文本

javascript - 计算 jquery php 中每个 div 的类数

javascript - IE-9 将占位符的值作为值