Javascript 插件工具提示-Trunkate标题

Javascript 插件工具提示-Trunkate标题,javascript,jquery,tooltipster,Javascript,Jquery,Tooltipster,我正在使用插件“tooltipster”,但我想将标题截断为30个字符并添加hellips 我有一个3个链接的列表。 下面是代码,并添加了到示例的链接 $('.tooltip').tooltipster({ animation: 'fade', delay: 200, touchDevices: false, trigger: 'hover', position: 'bottom', theme: 'tooltipster-shadow' });

我正在使用插件“tooltipster”,但我想将标题截断为30个字符并添加hellips

我有一个3个链接的列表。 下面是代码,并添加了到示例的链接

$('.tooltip').tooltipster({
    animation: 'fade',
    delay: 200,
    touchDevices: false,
    trigger: 'hover',
    position: 'bottom',
    theme: 'tooltipster-shadow'
});


$('.box a').each(function(){
    if ($(this).attr('title').text().length > 20) {
        $(this).attr('title').text($(this).text().substr(0, 17)); 
        $(this).attr('title').append(' ...');
    }

});

多谢各位

  • 应该在dom就绪后执行脚本,使用
    $(document).ready(function(){})
    $(function(){})
  • 要获取属性值,请使用
    $.attr('attribute')
    而不是
    $.attr('attribute').text()
  • 要更新属性值,请使用
    $.attr('attribute','new value')
    而不是
    $.attr('attribute').text('new value')
  • 您的新代码如下所示:

    $(function(){
    
        $('.box a').each(function(){
            var title = $(this).attr('title');
            if (title.length > 20) {
                $(this).attr('title', title.substr(0, 17) + '...'); 
            }
        })
    
        $('.tooltip').tooltipster({
            animation: 'fade',
            delay: 200,
            touchDevices: false,
            trigger: 'hover',
            position: 'bottom',
            theme: 'tooltipster-shadow'
        });
    
    })