Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
JavaScript: no se puede obtener tagName al hacer clic

Quiero obtener tagName de cualquier elemento dentro del iframe al hacer onclick . Sin embargo, no estoy siendo capaz de hacer eso. Soy nuevo en JavaScript. ¿Dónde estoy equivocado?

 <div class="flex-child green"> <p id="a">You clicked on: </p> <iframe id="zzz" srcdoc="<html><h1>hello</h1><button>click me!</button></html>" width="500" height="500" onload="clickfn()" ></iframe> </div> </div> <script> function clickfn() { x = document.getElementById("zzz").target.tagName; document.getElementById("a").innerHTML = x; } </script> <style> .flex-container { display: flex; } .flex-child.green { flex: 1; } </style>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Para el código que se muestra (usando srcdoc ), debería poder adjuntar el detector de eventos a la contentWindow de contenido del iframe :

 document.getElementById("zzz").addEventListener('load', iframeLoaded) function iframeLoaded() { document.getElementById("zzz").contentWindow.addEventListener('click', function(e) { document.getElementById("a").innerHTML = e.target.tagName; }) }
 .flex-container { display: flex; } .flex-child.green { flex: 1; }
 <div class="flex-child green"> <p id="a">You clicked on: </p> <iframe id="zzz" srcdoc="<html><h1>hello</h1><button>click me!</button></html>" width="500" height="500"></iframe> </div>

Si esto funciona o no depende de los encabezados de seguridad enviados por el servidor, aquí en StackOverflow no funciona. En jsfiddle.net funciona.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!