function replaceLink(href_array){
$('.externalLink').each(function(index){
$(this).replaceWith(function() {
if ($(this).text() != "") {
return $('<a />').append($(this).contents()).attr('href', href_array[index]);
}
else { // do not use
return $('<a />').append(href_array[index]).attr('href', href_array[index]);
}
});
});
};
the initial page has the following structure
body
p div.externalLink /p
p div.externalLink /p
p div.externalLink /p
p div.externalLink /p
p div.externalLink /p
p div.externalLink /p
/body
After the script should be
body
p a#href /p
p a#href /p
p a#href /p
p a#href /p
p a#href /p
p a#href /p
/body
but it turns out
body
p /p a#href
p /p a#href
p /p a#href
p /p a#href
p /p a#href
p /p a#href
/body
how that make?