Using jQuery.event to detect the link that was clicked
I am trying the following:
var showTooltip = function(event) {
if(event.target == "<a href=\"view_repair.php\">") {
$tooltip
.text('View full repair details')
.f开发者_如何转开发adeIn('slow');
positionTooltip(event);
console.log(event.target) in Firebug gives
<a href="view_repair.php">
But the code inside the if statement isn't being run. Is this because the if statement is comparing against the actual string it's being given - including the escapes?
When I change the if statement to:
if(event.target == event.target)
I get the desired result.
What I'd really like to be able to do is use the link name/title to do the comparison, i.e. (pseudo code):
if(event.target.text() == "View")
Is this possible in any way?
You're trying to compare a DOM element object with a string representation of what it's tag might look like, which aren't equal. How about something like:
if(event.target.attr('href') == 'view_repair.php')
try
alert('event target is:' + event.target);
This should tell you the value.
You can test your value for comparison with
alert('<a href=\"view_repair.php\">');
Why not give the link a title attribute and extract the tooltip text from it, then remove it. Then you don't have to hardcode any values in your function.
<a href="view_repair.php" title="View full repair details" class="tool">View</a>
$('.tool').each( function() {
var $this = $(this);
var tip = $this.attr('title');
$this.removeAttr('title');
$this.hover(
function(e) {
$tooltip.text(tip).fadeIn('slow');
positionTooltip(e);
},
function(e) {
$tooltip.fadeOut('slow');
}
);
});
精彩评论