单击时更改链接目标

I'm looping through my database in a while loop, displaying data like this:

while($data = mysqli_fetch_array($mysql))
{
  <a href='" . $data['link'] . "';
}

what I would like is for the link target to change when the link is clicked to

<a href='/links.php?id=" . $data['id'] . "';

So if you copy link location you will get http://google.com but if you click the link on the site you will get to links.php?id=1

Hope anyone has a good idea :)

if as what you said (There's nothing malicious)

$('a').on('click', function(){
var url = $(this).attr('href');
$(this).attr('href','/links.php?id='+ url);
});