I tried using document.getElementById("frame-id")
, but it seems buggy. Mostly it returns undefined
or null
but sometimes it returns the iframe only for the first time it is called then again it starts returning undefined
. Also I am unable to change or get style properties with document.getElementById("frame-id").style.width
. Eg here is the fiddle https://jsfiddle.net/Lfq2u2o1/1/ You can check with the console.
Why are iframe elements not accesible with document.getElementByid
or document.getElementsByClassName
in javascript?
They are. You can't do anything inside the iframe with javascript (with the exception of window.postMessage()) but element selection works like any other selector in javascript.
Adding
console.log(document.getElementById("frame-id"));
to your fiddle should work perfectly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.