Learning Horizon | For Learners

ASP.NET, SQL SERVER, JQUERY,JAVASCRIPT, WEBSPHERE

Thursday 20 September 2012

Apply CSS To Specific Element On Web Page Using Jquery

Jquery addClass() built-in method is used for this purpose. We can add style to certain elements/selected parts of our web page by using the addClass() method. We can use this method very easily as it takes the name of the class as a parameter and applies the CSS style defined in that class to a specific element.

For Example, the style rule is written as follows:


	<style type="text/css">
	.appTest{
		    font-style:italic;
		    font-size:16px;
		    background-color:#CCC;
                    text-align:justify;
                }
	</style>
			

Now I’ll show you how to apply the above CSS class to all p tags or paragraphs.

<script type="text/javascript"> $(document).ready (function () { $('p').addClass ('appTest'); }); </script>
 

$('p').addClass ('appTest'); In this line $(‘p’) select all p elements from the page and applies the style rules defined in the appTest class to them.

No comments:

Post a Comment

Please do not enter spam links.