У меня есть предыдущий jsfiddle. Прямо сейчас, если вы наведете "тестирование", вы увидите квадратный блок. Вы заметите в html, <a>
для тестирования не имеет rel. Что я добавляю, чтобы предотвратить добавление в текст, если нет rel, но все еще добавляется, если найден rel?
Код:
this.screenshotPreview = function(){
xOffset = 20;
yOffset = 30;
$("tr").hover(function(e){
var text = $(this).find('a').attr('rel');
$("body").append("<p id='screenshot'>"+ text +"</p>");
$("#screenshot")
.css("top",(e.pageY - yOffset) + "px")
.css("left",(e.pageX + xOffset) + "px")
.fadeIn("fast");
},
function(){
this.title = this.t;
$("#screenshot").remove();
});
$("a.screenshot").mousemove(function(e){
$("#screenshot")
.css("top",(e.pageY - yOffset) + "px")
.css("left",(e.pageX + xOffset) + "px");
});
};
// starting the script on page load
$(document).ready(function(){
screenshotPreview();
});
HTML:
<table>
<tr><td>content<a rel="Testing this out. This could be a long description. Blah blah blah. Testing this out. Testing this out. This could be a long description. Blah blah blah. Testing this out. " ></td></tr>
<tr><td><a>Testing</a></td></tr>
<table>
Измените селектор только на целевые элементы с rel
$("tr:has(a[rel])").hover(function(e){ ...
Попробуйте это для tr-hover:
$("tr").hover(function(e){
var text = $(this).find('a').attr('rel');
if ($.trim(text) === '') return;//exit if no text for append
$("body").append("<p id='screenshot'>"+ text +"</p>");
$("#screenshot")
.css("top",(e.pageY - yOffset) + "px")
.css("left",(e.pageX + xOffset) + "px")
.fadeIn("fast");
},
function(){
if (!this.t) return;
this.title = this.t;
$("#screenshot").remove();
});
tr:has(a[rel])
, tr
работает быстрее, чем tr:has(a[rel])
(это приведет к Sizzle)