// Wait for the document to be ready
// Find the iframe element by its ID
var iframe = $('#myFrame');
// Access the iframe's head element
var iframeHead = iframe.contents().find('head');
// Create a new style element
var styleElement = $('<style>')
.attr('type', 'text/css')
.text('body { background-color: yellow; }');
// Append the style element to the iframe's head
iframeHead.append(styleElement);
});
In the above example, we first wait for the document to be ready using $(document).ready() function.
Then, we find the iframe element by its ID using $('#myFrame').
We access the iframe's head element using contents().find('head').
Next, we create a new style element using $('') and set its attributes and text content. Finally, we append the style element to the iframe's head using append().
Make sure the iframe source is from the same domain to avoid cross-origin issues.
If you are using a 3rd party library to manipulate the contents of the and want to append a style to its head element, you'll need to ensure that the library has provided a way to access and modify the contents of the iframe.
Typically, if the library supports iframe manipulation, it should provide a method or API to access the iframe's document or contents. Once you have access to the document, you can use regular JavaScript or jQuery to modify its contents.
It may be blocked for access as well.