这应该这样做
var cast = 'jack, kate ,sawyer , john lock ,, hurley';
var castlist = cast.split(',');
var $ul = $('<ul>'); //create an in-memory <ul> element to hold our elements
$.each(castlist, function(idx,val){ // for each item in the split array
var value = $.trim(val).replace(/\b\S/g, function(m){return m.toUpperCase();}); // trim and capitalize the item
if (value.length > 0){ // if its length > 0 (non-empty)
var $li = $('<li>', { // create a <li> element
html: $('<a>', { // set its html to be a new <a> element
href:'Bio.html#'+value, // with a href
text:value // and a text value
})
});
$ul.append($li); // append our new <li> to the <ul>
}
});
$('.lost-cast').append( $ul ); // append the filled <ul> in the /lost-cast element
演示http://jsfiddle.net/gaby/gvb6n/