简体   繁体   中英

If…Else: If iFrame exists, apply CSS to page?

I'm new here. And very new to programming, markup, web dev. I do have some basic understanding of HTML and CSS, but this is my first attempt at using jQuery/Javascript.

Is it possible to use jQuery (or otherwise) to

1- detect if an iframe with class="iframe1" appears on a given page and

2- if it does exist, to remove the primary navigation bar with id="main-header" ?

I'm currently using this custom CSS to hide the navigation globally:

#main-header {
display:none !important;
}

and then using this (for example) to replace it on individual pages:

.page-id-1350 #main-header {
display:block !important;
}

Obviously this is not ideal, and I very much like to automate this process.

Thanks very much in advance,

Chris

You can use the following jQuery code to do just that:

if($(".iframe1").length > 0) // If .iframe1 exists
    $("#main-header").hide(); // Hide #main-header

Or, go the opposite way, keep your CSS that hides #main-header and then show it when .frame1 is not present on the page:

if($(".iframe1").length == 0) // If .iframe1 does not exists
    $("#main-header").show(); // Show #main-header

You could do this with JavaScript, with code like this:

<script>
// Place this at the end of the body
window.addEventListener("load", function(){
    var iframeTest = document.querySelectorAll("iframe.iframe1");
    var navBar = document.getElementById("main-header");
    if (iframeTest.length > 0) {
        navBar.setAttribute("style","display:none");
    }
});
</script>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM