The theLink variable you create within the for loop is a jQuery object (the result of the call to .appendTo() is still the created link).
You can, as usual, call .click(<function>) on it to bind a function to the click event.
Example:
$data.append("<tfoot><tr><td></td</tr></tfoot>"); // added > at the end of the string
$("#tab2").html($data);
alert(data.n)
for (var i = 0; i < data.n + 1; i++) { // added var before i
var thelink = $('<a>', { // added var before thelink
text: i,
href: '#'
}).appendTo('tfoot');
// Now you can add the click event:
thelink.click(function () {
alert("Hello, you clicked the link number "+i);
});
}
On a side note, maybe you should call .appendTo('#tab2 tfoot') instead of just .appendTo('tfoot') - the latter will add the link to all tfoots on the page, the former just to #tab2's tfoot.