我认为问题源于新的 qTip 版本和 jQuery 之间的不兼容。
我花了最后一个小时使用 qTip 测试代码,试图找出我的代码拒绝工作的原因,并在查看 forums 以查看是否可以找到类似的问题后,我注意到以下代码在该线程运行良好,但如果它与更新版本的 jQuery 交换,则会产生错误。
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.qtip-1.0.0-rc3.min.js"></script>
<script type="text/javascript">
$( document ).ready( function( ) {
$.fn.qtip.styles.tooltipDefault = {
background : '#132531',
color : '#FFFFFF',
textAlign : 'left',
border : {
width : 2,
radius : 4,
color : '#C1CFDD'
},
width : 220
}
// we are going to run through each element with the classRating class
$( '.classRating' ).each( function( ) {
var rating = $( this ).attr( 'rating' );
// the element has no rating tag or the rating tag is empty
if ( rating == undefined || rating == '' ) {
rating = 'I have not yet been rated.';
}
else {
rating = 'The rating for this is ' + rating + '%';
}
// create the tooltip for the current element
$( this ).qtip( {
content : rating,
position : {
target : 'mouse'
},
style : 'tooltipDefault'
} );
} );
} );
</script>
</head>
<body>
<div id="SomeID1" class="classRating" rating="73">I have a rating</div>
<div id="SomeID2" class="classRating" rating="66">I have a rating</div>
<div id="SomeID3" class="classRating" rating="">I have a rating but it is empty</div>
<div id="SomeID4" class="classRating">I dont have a rating</div>
</body>
</html>
我下载了jQuery 1.3.2 from the Google Code website,这个例子现在非常适合我。我很快就会开始根据我的需要定制这段代码,看看是否还有其他问题,但对于仍然遇到这个问题的人,我希望这篇文章有用。
编辑: 看起来是版本不兼容问题。文档网站上的这个简单代码现在可以与这些相同的版本完美配合。希望对您有所帮助!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<a href="#" title="Hello World" class="example">Test</a>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.qtip-1.0.0-rc3.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// By suppling no content attribute, the library uses each elements title attribute by default
$('a[href][title]').qtip({
content: {
text: false // Use each elements title attribute
},
style: 'cream' // Give it some style
});
// NOTE: You can even omit all options and simply replace the regular title tooltips like so:
// $('#content a[href]').qtip();
});
</script>
</body>
</html>
编辑 2: 有一个新版本的 qTip 正在开发中,所以可能值得等待,但是如果你想将 qTip 与最新版本的 jQuery 一起使用,那么你可以下载一个qTip 的nightly builds。使用上面的示例,我将两个脚本换成了最新的 jQuery (1.4.2) 和最新的 nightly build,它似乎可以工作。