Using CSS to affect div style inside iframe

前端 未结 13 1562
失恋的感觉
失恋的感觉 2020-11-22 07:00

Is it possible to change styles of a div that resides inside an iframe on the page using CSS only?

相关标签:
13条回答
  • 2020-11-22 07:03

    You can retrieve the contents of an iframe first and then use jQuery selectors against them as usual.

    $("#iframe-id").contents().find("img").attr("style","width:100%;height:100%")
    
    $("#iframe-id").contents().find("img").addClass("fancy-zoom")
    
    $("#iframe-id").contents().find("img").onclick(function(){ zoomit($(this)); });
    

    Good Luck!

    0 讨论(0)
  • 2020-11-22 07:06

    Apparently it can be done via jQuery:

    $('iframe').load( function() {
        $('iframe').contents().find("head")
          .append($("<style type='text/css'>  .my-class{display:none;}  </style>"));
    });
    

    https://stackoverflow.com/a/13959836/1625795

    0 讨论(0)
  • 2020-11-22 07:10

    Combining the different solutions, this is what worked for me.

    $(document).ready(function () {
        $('iframe').on('load', function() {
            $("iframe").contents().find("#back-link").css("display", "none");
        }); 
    });
    
    0 讨论(0)
  • 2020-11-22 07:10

    Yes, it's possible although cumbersome. You would need to print/echo the HTML of the page into the body of your page then apply a CSS rule change function. Using the same examples given above, you would essentially be using a parsing method of finding the divs in the page, and then applying the CSS to it and then reprinting/echoing it out to the end user. I don't need this so I don't want to code that function into every item in the CSS of another webpage just to aphtply.

    References:

    • Printing content of IFRAME
    • Accessing and printing HTML source code using PHP or JavaScript
    • http://www.w3schools.com/js/js_htmldom_html.asp
    • http://www.w3schools.com/js/js_htmldom_css.asp
    0 讨论(0)
  • 2020-11-22 07:11

    probably not the way you are thinking. the iframe would have to <link> in the css file too. AND you can't do it even with javascript if it's on a different domain.

    0 讨论(0)
  • 2020-11-22 07:16

    Just add this and all works well:

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    
    0 讨论(0)
提交回复
热议问题