简体   繁体   中英

Jquery first word CAPs

I am using wordpress the_date() function to get the post date, and it returns me

<h2 class="the_date>Friday, August 12, 2011</h2>

What I need is to have the friday to be caps without effecting other words aka FRIDAY, August 12, 2011.

Can this be done with jQuery or javascript?

Yes, using JavaScript's String.toUpperCase() :

var date = "Friday, August 12, 2011";
var pieces = date.split(" ");
pieces[0] = pieces[0].toUpperCase();
alert(pieces.join(" "));

Demo.

Keep in mind, this is pseudocode, not checked for syntax/runnability.

var myString = the_date()
myString = myString.SubString(0, myString.indexOf(',')).ToUpperCase() + myString.Substring(myString.indexOf(','));

The syntax may be off a bit, and you may need to add/subtract 1 on either of the indexOf calls to include the proper character, but this method should work.

You'd have to use either substr/substring or split/join. I'd be inclined to use split/join:

// breaks into array based on comma
var dt = 'Friday, August 12, 2011'.split(','); 
// reassign 0 index to upper case (so Friday, in this case)
dt[0] = dt[0].toUpperCase();
console.log( dt.join( ',' ) ); // FRIDAY, August 12, 2011

And here's the substr version:

var dt = 'Friday, August 12, 2011';
// I'm using 'y' here because days which do not end in Y are very rare.
var yindex = dt.indexOf("y");
     // everything before and including y to upper case
dt = dt.substr(0, yindex + 1).toUpperCase() + 
     // then add the balance to the end.
     dt.substr( yindex + 1);
console.log( dt );

javascript can do this. you can substring the first word out, capatalize it, and put it back.

In your WordPress theme, you could use

<?php strtoupper(the_time('l')) . ", " . the_time('F j, Y'); ?>

instead of the_date() to get your desired output.

Update

jQuery solution if you wish to go that route:

HTML

<h2 class="the_date">Friday, August 12, 2011</h2>

jQuery

var the_date = $('.the_date').html().split(', ');
the_date = [the_date[0].toUpperCase(), the_date.slice(1).join(', ')].join(', ');
$('.the_date').html(the_date);

jsFiddle demo: http://jsfiddle.net/tcA4y/

Updated jsFiddle demo : http://jsfiddle.net/tcA4y/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM