2015-03-19 56 views
0

jQuery UI API继工具提示教程我试图创建自定义html内容的工具提示,但没有显示出来。控制台显示我的脚本没有错误,只有一些来自jQuery-UI(但没有特别的)。jQuery的自定义内容工具提示什么都没有

任何人都可以告诉我我做错了什么吗?

$(document).ready(function() { 
 
    $(function() { 
 
    $(document).tooltip({ 
 
     items: "custom-tooltip", 
 
     content: function() { 
 
     var element = $(this); 
 
     if (element.is("custom-tooltip")) { 
 
      return "<a>THIS<br/>IS<br/><A><br/>TOOLTIP!"; 
 
     } 
 
     } 
 
    }); 
 
    }); 
 
});
<!doctype html> 
 
<html lang="en"> 
 

 
<head> 
 

 
    <meta charset="UTF-8"> 
 
    <title>CUSTOM TOOLTIPS</title> 
 

 
    <link rel="stylesheet" href="jquery-ui.css"> 
 

 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
    <script src="jquery-1.10.2.js"></script> 
 
    <script src="jquery-ui.js"></script> 
 

 
    <script src="tooltips.js"></script> 
 

 
</head> 
 

 
<body> 
 

 
    <h3><a href="#" custom-tooltip="">HOVER ME</a></h3> 
 

 
</body> 
 

 
</html>

回答

1

.is功能需要的选择器。如果它存在匹配的属性,围绕着它在方括号[]

if (element.is("[custom-tooltip]")) { 

同样与items选项:

 items: "[custom-tooltip]", 

你的代码的其余部分看起来不错。请注意,由于items已将小部件限制为相同的标签,因此您可能根本不需要if检查。

的有效选择更多信息可以在文档页面上找到:http://api.jquery.com/category/selectors/

+0

嗯,这就是我想要它的情况下,使用简单的东西时,如果我想在多个元素上使用多个工具提示,或者有更简单的方法,那么IF是否适用?就像我对5个不同元素有5个不同的工具提示一样,我会为每个元素做出判断,如果是句子 – 2015-03-19 11:28:06

+0

传统的方式是使用“title”。但是,如果你动态地加载内容,你拥有的是一个很好的简单的方法。 [jqueryui页面上的一个例子](https://jqueryui.com/tooltip/#custom-content)也一样 – blgt 2015-03-19 11:32:47

1

看到这个JSFiddle。您在工具提示设置中犯了一些错误。 工具提示项设置应该是这样的:items: "[custom-tooltip]" ,为什么你在呼唤记录功能齐全这样

$(document).ready(function() { -- document ready event 
    $(function() { -- same document ready 
    $(document).tooltip(...); 
    }); 
}); 

jQuery代码会是这样:

$(function() { 
    $(document).tooltip({ 
     items: "[custom-tooltip]", 
     content: function() { 
     var element = $(this); 
     if (element.is("[custom-tooltip]")) { 
      return "<a>THIS<br/>IS<br/><A><br/>TOOLTIP!"; 
     } 
     } 
    }); 
});