Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding css file with jquery

I am creating a popupwindow and I want to add a css file to that popupwindow. Below is the code for popupwindow. I have a JavaScript which creates a popupwindow.

<a href="popupwindowcontent.xhtml" title="Print" class="popupwindow">Print1</a> 

Now I want to add a css file to this popupwindow. I tried something like

$('.popupwindow').append('<link rel="stylesheet" href="css/style2.css" type="text/css" />');    $('head').append('<link rel="stylesheet" href="css/style2.css" type="text/css" />'); 

but it doesn't work.

like image 685
Jay Avatar asked Apr 15 '11 18:04

Jay


People also ask

Can we add CSS in jQuery?

jQuery has several methods for CSS manipulation. We will look at the following methods: addClass() - Adds one or more classes to the selected elements. removeClass() - Removes one or more classes from the selected elements.

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.

How do I import a file into CSS?

The @import rule allows you to import a style sheet into another style sheet. The @import rule must be at the top of the document (but after any @charset declaration). The @import rule also supports media queries, so you can allow the import to be media-dependent.

How can give multiple CSS properties in jQuery?

Apply multiple CSS properties using a single JQuery method CSS( {key1:val1, key2:val2....). You can apply as many properties as you like in a single call. Here you can pass key as property and val as its value as described above.


2 Answers

$('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />'); 

This should work.

like image 180
Etienne Dupuis Avatar answered Sep 21 '22 01:09

Etienne Dupuis


This is how I add css using jQuery ajax. Hope it helps someone..

$.ajax({             url:"site/test/style.css",             success:function(data){                  $("<style></style>").appendTo("head").html(data);             }         }) 
like image 35
Dilip Rajkumar Avatar answered Sep 21 '22 01:09

Dilip Rajkumar