简体   繁体   English

使用jquery设置内部iframe内div的宽度,而iframe上没有ID

[英]set the width of a div inside an inner iframe using jquery *without* having an id on the iframe

I have the following code: 我有以下代码:

<iframe>
  <div id="inner_div">
    Here's some text!
  </div>
</iframe>

I know that if I had an id on the iframe, I could follow the steps here to get an accessor to "#inner_div" and I could just set the height/width from there. 我知道,如果我在iframe上有一个ID,则可以按照此处的步骤操作以获取“ #inner_div”的访问器,并且可以从此处设置高度/宽度。

I also know that I could use a tag selector as stated here if I don't have a parent id. 我也知道,如果没有父ID,我可以使用此处所述的标签选择器。

But the problem with the second method is that when I call 但是第二种方法的问题是当我打电话时

$('iframe')[0]

in my code, it returns an HTMLIFrameElement, which isn't a jQuery object (so I can't use .find or any other jQuery magic on it). 在我的代码中,它返回一个HTMLIFrameElement,它不是jQuery对象(因此我不能在其上使用.find或任何其他jQuery魔术)。 I've also tried wrapping that in text and passing it to the jQuery object to re-parse it as a DOM element, but that doesn't work either. 我也尝试过将其包装在文本中并将其传递给jQuery对象,以将其重新解析为DOM元素,但这也不起作用。

So is there any way to still get jQuery goodness to run on the inside of the iFrame? 因此,有什么方法可以让jQuery优势在iFrame的内部运行吗?

$($('iframe')[0])

will turn your first iframe into a jQuery object. 会将您的第一个iframe变成jQuery对象。

A more jQuery-ish solution would be 一个更像jQuery的解决方案是

$('iframe').first()

You should note that (unless your iFrame loads content from the same domain) you are very likely to run into cross domain policy issues (which, in short, means that you won't be able to access the iFrame's content). 您应该注意(除非您的iFrame从同一域中加载内容),您很可能会遇到跨域策略问题(简而言之,这意味着您将无法访问iFrame的内容)。

$($('iframe')[0]).contents().find('#inner_div').css("width", "some_value");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM