Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-21-2011, 04:30 PM   PM User | #1
minkoko
New Coder

 
Join Date: Aug 2010
Location: myeik
Posts: 72
Thanks: 4
Thanked 5 Times in 5 Posts
minkoko can only hope to improve
Unhappy function return

hi,i don't understand about the jquery basic example
the code is below
PHP Code:
<html>
<
head>
<
script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $('p').addClass(function(n){
      return 'par_' + n;
    });
  });
});
</script>
<style type="text/css">
.par_0
{
color:blue;
}
.par_1
{
color:red;
}
</style>
</head>

<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Add classes to p elements</button>
</body>
</html> 
i want to understand about the how is work function(n){ in the function a
and how work return function return 'par_' + n;
thanks mate
minkoko is offline   Reply With Quote
Old 09-21-2011, 06:10 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
This is one of the possible ways to use the addClass method: The first parameter of the function "n" is the index of the current element out of the jQuery element list

Let's say you have 10 <p> paragraphs on your page. Then the selector $('p') will result in a list of those paragraphs. addClass(function(n) {...}) will then add a class to each paragraph containing the text par_n. So the first paragraph will have a new class="par_0"
devnull69 is offline   Reply With Quote
Old 09-21-2011, 09:33 PM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
i guess mopst methods map to .map(), which i didn't know.
so, the long-hand would be:

Code:
 $('p').map(function(n, elm){
      $(elm).addClass('par_' + n);
    });
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Old 09-22-2011, 12:29 PM   PM User | #4
minkoko
New Coder

 
Join Date: Aug 2010
Location: myeik
Posts: 72
Thanks: 4
Thanked 5 Times in 5 Posts
minkoko can only hope to improve
thanks for explain my dearly friend,
minkoko is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:06 AM.


Advertisement
Log in to turn off these ads.