I'm using below code for replacing links:
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, it 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 like this:
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 can I correct this?