2011-08-19 29 views
0

我尝试使用qTip jQuery插件在这里:http://craigsworks.com/projects/qtip2/demos/#mousejQuery的qTip插件,如何分割“标题”属性和应用样式

我的代码基础上的演示工作,但我想能够从TITLE属性中“爆炸”内容,并将数组的第一项作为“标题”,将第二项作为“内容”。

我有以下代码:

<script> 
$(document).ready(function() 
{ 

    $('.tip3').qtip({ 
     content: { 
      text: function(api) { 
        var titleAttr = $(this).attr('title'); 
        var titleAttrArray = titleAttr.split(' :: '); 
        return titleAttrArray[1]; 
       }, 
      title: { 
       text: function(api) { 
        // Retrieve content from TITLE attribute of the $('.selector') element 
        // return $(this).attr('title'); 
        return titleAttrArray[0]; 
       } 
      } 
     }, 
     position: { 
     my: 'top left', 
     target: 'mouse', 
     viewport: $(window), // Keep it on-screen at all times if possible 
     adjust: { 
      x: 10, y: 10 
     } 
     }, 
     hide: { 
     fixed: true // Helps to prevent the tooltip from hiding ocassionally when tracking! 
     }, 
     style: 'ui-tooltip-youtube' 
    }); 

}); 
</script> 


<div class="box tip3" title="Some cool title here! :: Some subheader here?"> 
    <h3>Hover over this box to see mouse tracking in action</h3> 
</div> 

仅供参考,下面的代码的工作原理是刚取TITLE完美的罚款,并没有做任何事情与它:

<script> 
$(document).ready(function() 
{ 
    $('.tip2').qtip({ 
     content: { 
      text: 'I really like owls!', 
      title: { 
       text: function(api) { 
        // Retrieve content from TITLE attribute of the $('.selector') element 
        return $(this).attr('title'); 
       } 
      } 
     }, 
     position: { 
     my: 'top left', 
     target: 'mouse', 
     viewport: $(window), // Keep it on-screen at all times if possible 
     adjust: { 
      x: 10, y: 10 
     } 
     }, 
     hide: { 
     fixed: true // Helps to prevent the tooltip from hiding ocassionally when tracking! 
     }, 
     style: 'ui-tooltip-youtube' 
    }); 

}); 
</script> 

任何想法/见解会太棒了!仍然是一个尝试将jQuery绘制出来的新手:)。

回答

1

您不能在标题函数中访问titleAttrArray,因为它不在范围内。只需处理标题属性两次或使用其他属性。

$('.tip3').qtip({ 
    content: { 
     text: function(api) { 
      return $(this).attr('title').split(' :: ')[1]; 
     }, 
     title: { 
      text: function(api) { 
       return $(this).attr('title').split(' :: ')[0]; 
       //OR 
       return $(this).attr('title_header'); 
      } 
     } 
    } 
}); 
+0

向HTML标记添加随机属性可以吗? “title_header”不存在我明白...? – Jay

+0

这似乎是辩论:http://forum.jquery.com/topic/jquery-creating-custom-attributes-in-html – Jay

+0

个人而言,我不喜欢使用非标准属性,但我将它添加为它是一种替代方法。 –