如何使用jquery在iframe中选择代码? [英] how to select tags within an iframe using jquery?

查看:60
本文介绍了如何使用jquery在iframe中选择代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在生成的iframe中选择并修改HTML. iframe可显示各种媒体(图像,pdf等).为了显示不同的项目,我最初使用类似这样的内容来创建它:

I'm trying to figure out how to select, and then modify, the HTML within an iframe I generate. The iframe displays various media (images, pdfs, etc.). To show different items, I initially create it using something like this:

$('#mydiv').html("<iframe id='myiframe' src='path/file.jpg'></iframe>");

,然后根据需要使用以下内容更新其内容:

and then, as needed, update its contents using something like this:

$('#myiframe').attr("src","path/newfile.jpg");

这将生成如下HTML(通过Chrome的元素查看器查看):

this produces HTML like this (as seen through Chrome's elements viewer):

<div id='mydiv'>
  <iframe id='myiframe' src='path/file.jpg'>
    #document
      <html>
        <body style="margin:0">
          <img style="-webkit-user-select:none" src="path/file.jpg">
        </body>
      </html>
  </iframe>
</div>

我想选择动态生成的<img>标签,以便调整其宽度.但是我不知道要使用jquery选择器来做到这一点.想法?

I want to select that dynamically generated <img> tag so that I can adjust its width. But I can't figure out the jquery selector to do it. Ideas?

推荐答案

使用jQuerys contents 方法.文档甚至有一个标题为更改iframe内链接的背景颜色"的示例.

Use jQuerys contents method. The docs even has an example titled "Change the background colour of links inside of an iframe."

获取匹配元素集中每个元素的子元素, 包括文本和注释节点.

Get the children of each element in the set of matched elements, including text and comment nodes.

应用于您的代码:

var images = $('#myiframe').contents().find('img');

真正的问题是,为什么首先需要iframe?

The real question is why do you need the iframe in the first place?

这篇关于如何使用jquery在iframe中选择代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆