質問

I'm coming here after a few hours of outstanding rage, anger, shock - perhaps just plain incredulity - at what's happened to me here.

I've attempted to create the simplest of functions in a lengthy script - get an element by Id, then change part of the class depending on a variable. Sounds easy right? Here's the relevant code:

{literal}
// check to see what kind of ban is this based on the ban_until and ban_at columns in table banlist from return data of /inc/classes/bans.class.php
var banduration = {/literal}{$ban.until - $ban.at}{literal};
if (banduration !== 1 || 0) {
    var bantype = ((banduration < 0 || banduration > 3600) ? "ban" : "warning");
}
// replace all classnames with others in case this is a warning/ban, and END the script before it also changes modnames
if (bantype === "warning" || "ban") {
            document.getElementbyId("modname_message_background").className = "common_background " + bantype + "_background";
            document.getElementById("modname_message_bottomribbon").className = "common_bottomribbon " + bantype + "_bottomribbon";
            document.getElementById("modname_message_letterbox").className = "common_letterbox " + bantype + "_letterbox";
            document.getElementById("modname_message_modname").className = "common_modname " + bantype + "_modname";
            document.getElementById("modname_message_servertime").className = "common_servertime " + bantype + "_servertime";
            document.getElementById("modname_message_signature").className = "common_signature " + bantype + "_signature";
            document.getElementById("modname_message_topribbon").className = "common_topribbon " + bantype + "_topribbon";
            document.getElementById("modname_message_username").className = "common_username " + bantype + "_username";
        }

This is fairly self-explanatory: This is in a Smarty template, and $ban.until is the unix time of a ban's end, and $ban.at is the unix time that it was applied, and so on and so on. But as I ran this script, which is designed to change the ban message depending on individual moderator ranks (later on but I digress) and the message type (message, warning, or ban). When I inserted this, only the first line was used. Agitated, I spent two hours reworking it multiple times in different ways to absolutely no avail. Furious with myself, I wrote this:

if (bantype == "warning" || "ban") {
    var list = ["modname_message_background","modname_message_bottomribbon","modname_message_letterbox","modname_message_modname","modname_message_servertime","modname_message_signature","modname_message_topribbon","modname_message_username"];
    var secondlist = ["background","bottomribbon","letterbox","modname","servertime","signature","topribbon","username"];
    for (var i=0;i<list.length;i++) {
    document.getElementById(list[i]).className = "common_" + secondlist[i] + " " + bantype + "_" + secondlist[i];
    }
}
return;

This didn't work either. I was incredulous - defeated, I come here, pleading for the ludicrously simple mistake I had to have missed, because only something so simple can be such a nuisance.

I can confirm that variable banduration is working perfectly (using alert).

役に立ちましたか?

解決

if (bantype === "warning" || "ban")

doesn't do what you think it does. It's equivalent to:

if ((bantype === "warning") || "ban")

which is always true, because "ban" is not false. It should be:

if (bantype === "warning" || bantype === "ban")

And:

if (banduration !== 1 || 0)

should be:

if (banduration !== 1 && banduration !== 0)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top