如何从iframe访问文档中的<img rel="nofollow noreferrer" />?

Jos*_*eph 6 javascript iframe jquery html5 css3

在此输入图像描述我有一个iframe

<iframe src="../Images/landingpage.jpg" id="Iframe1" name="ifrmContent" class="ifrmClass">
</iframe>
Run Code Online (Sandbox Code Playgroud)

我在inspector元素中发现img标签位于带有body标签的"#document"中

<html>
    <body style="margin: 0px;">
        <img style="-webkit-user-select: none;" src="../Images/landingpage.jpg">
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我需要访问这个img("landingpage.jpg")以便更改(图像很小......需要调整大小),其宽度为100%,高度为:90%

我试过用 #Iframe1>#document>html>body>img{width:100%;}

Tus*_*har 8

尝试

$("#Iframe1").contents().find("img").css({
    'width': '100%',
    'height': '90%'
});
Run Code Online (Sandbox Code Playgroud)

的CSS()

.找()

.内容()


Ami*_*oki 7

你可以这样做:

$('#Iframe1').contents().find('img').css({'width':'100%','height':'90%'});
Run Code Online (Sandbox Code Playgroud)

.contents()将为您提供iframe的内容并.find()找到您的图像,并.css()用于将样式应用于找到的图像.


小智 5

非 jQuery 选项

这个问题非常类似于 这一个

根据上面链接上的回答问题,您可以尝试:

var image = window.frames['Iframe1'].document.getElementByTagName('img')[0];
image.styles.width = '100%';
image.styles.height = '90%';
Run Code Online (Sandbox Code Playgroud)