javascript - 更改多个 iframe 的内容

标签 javascript jquery html iframe

我有一些项目需要使用一些数组更改多个 iframe 示例

<ul>
  <li>
    <iframe src="test.html">
      <head></head>
      <body>
        <div class="content">
          <p>test</p>
        </div>
      </body>
    </iframe>
  </li>
  <li>
    <iframe src="test.html">
      <head></head>
      <body>
        <div class="content">
          <p>test</p>
        </div>
      </body>
    </iframe>
  </li>
</ul>

我需要改变<div class="content"><p>test</p></div>对于每个 iframe使用数组 ["<h1>tittle</h1>","<h1>tittle2</h1>"] .

对于第一个 iframe,它将被替换为 array[0] 中的 HTML和第二个 iframe array[1] .

有人可以帮忙吗?谢谢

最佳答案

尝试使用 replaceWith()

var array = ["<h1>tittle</h1>", "<h1>tittle2</h1>"]

$(document).ready(function() {
  $('iframe').load(function() {
    $('iframe').each(function(a) {
      $(this).contents().find('body').children('.content').replaceWith(array[a]);
    })
  })

})

关于javascript - 更改多个 iframe 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43976762/

相关文章:

java - "bubble clusters"项目(可拖动性、分组、绘图、合并组)

javascript - 保存对 html Canvas 路径的引用

Javascript/jQuery 图像缩放插件

html - 导航不缩放

javascript - 访问从另一个文件加载的 id

javascript - 如何在 jQuery 中停止 onChange 事件

javascript - 将鼠标悬停在 div "rectange"上不起作用

javascript - 为什么我在 React Redux reducer 中收到有关合成事件的警告?

javascript - 使用 ScrollMagic 在 iOS Safari 上的全高视差页面上滚动时,引脚会跳​​动

javascript - 在 while 循环中使用 jQuery AJAX 请求