kazanova84,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(function() {
var context = "#context", selector = "table a";
var target = $(selector).filter(function(i,el) {
return $(context).find(el).length
}).add(context);
alert($.map(target,function(el) {
return el.tagName
}));
});
</script>
</head>
<body>
<div id="body">
<table id="context">
<tr>
<td><a>target</a></td>
</tr>
</table>
</div>
<div id="body">
<table id="test">
<tr>
<td><a>target</a></td>
</tr>
</table>
</div>
</body>
</html>