最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Getting the img tag inside iframe - Stack Overflow

matteradmin5PV0评论

I have an iframe with a class name="blaclass". Inside it, I have one div with no name (or id) and inside this id there is a div with class name = "classs". Inside this div, lie two images (with no class name/id).

How can I get those image.src? If there is a Prototype version even better!

PS: I cannot add class names/ids before the page renders fully (I am not creating the iframe - it's created dynamically via javascript and I run my script AFTER that).

Thx

I have an iframe with a class name="blaclass". Inside it, I have one div with no name (or id) and inside this id there is a div with class name = "classs". Inside this div, lie two images (with no class name/id).

How can I get those image.src? If there is a Prototype version even better!

PS: I cannot add class names/ids before the page renders fully (I am not creating the iframe - it's created dynamically via javascript and I run my script AFTER that).

Thx

Share Improve this question asked Jan 29, 2009 at 9:59 Jon RomeroJon Romero 4,1006 gold badges38 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

a.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en" lang="en">
<body onLoad="foo()">
  <iframe src="b.html">
    </iframe>
<script type="text/javascript">
function foo()
{
alert(window.frames[0].document.getElementsByClassName("classs")[0].getElementsByTagName("img")[0].src);
}
</script>
</body>
</html>

b.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml" xml:lang="en" lang="en">
<body>
   <div>
   <div class="classs">
   <img id="img" src="test.png" />
   <img id="img" src="test.png" />
   </div>
   </div>
</body>
</html>

Also be sure the domain which the iframe is on is the same as the one its pointing to. Otherwise you won't be able to get any information from the iframe.

Post a comment

comment list (0)

  1. No comments so far