I want to write a code that retrieves an element in iframe and adds css when clicked.

Asked 2 years ago, Updated 2 years ago, 47 views

I'd like to get an element in iframe and write the code where z-index:1 is added to the iframe where the element was clicked. There was no change in the code below.
Please let me know.

<iframe id="Hello" src="https://example.jp/header.php"></iframe>

<script>

// Get iframe element
variframeElem= document.getElementsByTagName('iframe');
variframeDocument=iframeElem[0].contentDocument||iframeElem[0].contentWindow.document;
// Get IDname element in page loaded with iframe
varpElem=iframeDocument.getElementsById('sss')[0];
Add iframe.style.zIndex=1 when the IDnamess in the page loaded with //iframe is clicked
$(document).on("click", "sss", function(){
iframe.style.zIndex=1;
});
</script>

javascript php laravel

2022-09-30 16:33

1 Answers

$(document).on("click", "sss", function(){

This means <sss> when the tag is clicked in a document outside of iframe.pElem may have been used as follows:

pElem.addEventListener("click", function(){

You will also need to wait for the iframe to load.All code after arvariframeDocument=... ん should be wrapped in a function as follows:

iframeElem[0].addEventListener('load', function(){
    variframeDocument=iframeElem[0].contentDocument||iframeElem[0].contentWindow.document;
    // Get IDname element in page loaded with iframe
    ...
});


2022-09-30 16:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.