With the code given below for a Tampermonkey script, I am pretty sure that if the return value is -1 (as it will default to be, given the example selection below), then there should be no Qtip made.
However, this code still creates Qtips on the elements which would have otherwise defaulted with a text value of -1. Could someone please explain why this is happening?

// headers that work fine

$( function() {
    GM_addStyle(GM_getResourceText('qtipCSS'));

    var makeConfig = function(jqObject){            
        var playerName = $(jqObject).text() || -1;
        var argumentMsg = '<a href="javascript:openQuickMsgDialog(' + '&quot;' + playerName + '&quot;' + ');" style="font-size:10px; "> [m] </a>'
        var argumentBuff = '<a href="javascript:openWindow(' + '&quot;' + 'index.php?cmd=quickbuff&t=' + playerName + '&quot;' + ', \'fsQuickBuff\', 618, 1000, \',scrollbars\');" style="font-size:10px;"> [b] </a>'

        var finalText = playerName + '<br/>' + '<div style = "text-align: center; color: white !important;">' +  argumentMsg + argumentBuff + '</div>';

        var qtipContent = {
            overwrite: false,
            content: { text: finalText, attr: 'error' },
            position: { my: 'bottom center', at: 'top center' },
            show: { delay: 200 },
            hide: { fixed: true, delay: 200 },
            style: { classes: 'qtip-tipsy qtip-shadow' }
        }
        return qtipContent;
    };

    $('[href *= "index.php?cmd"]').each(function(){     
        var config = makeConfig($(this));
        if (config.content.text == -1)
            return;
        else if (config.content.text != -1){
            $(this).qtip(config);
        }       
    });
});

Example proper selection (that's supposed to give me a player name and does):

$('[href *= "index.php?cmd"]').eq(1)

[
<a href=​"index.php?cmd...">​NameOfPlayer​</a>​
]

Example of a selection which should be processed to -1 and then be not created as a tooltip, but isn't.

$('[href *= "index.php?cmd"]').eq(0)

[
<a href=​"index.php?cmd..." data-hasqtip=​"0">​
<img src=​"someplace">​
</a>​
]

Note, I've removed some of the inline tags in both the <a> and <img> tags respectively. I don't think this should play any role since $(string).text() is supposed to remove tags completely, but if necessary I can add the tags again.

Thanks!

有帮助吗?

解决方案

The finalText logic is wrong and will never return just -1.

Don't filter the Qtip that way, anyway. Much better to do it in the calling loop. Change this:

$('[href *= "index.php?cmd"]').each(function(){     
    var config = makeConfig($(this));
    if (config.content.text == -1)
        return;
    else if (config.content.text != -1){
        $(this).qtip(config);
    }       
});


To this:

$('[href *= "index.php?cmd"]').each(function(){     
    if (this.textContent.trim () ) {
        var config = makeConfig ( $(this) );
        $(this).qtip(config);
    }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top