簡體   English   中英

如何顯示我的價值? 使用 document.getElementById

[英]How to display my value? using document.getElementById

您好,您能幫幫我嗎,我的錯誤是 [object Object] 我很難解決這個問題 我想在 id="display" 中顯示結果以顯示隨機數/值轉換為秒、分鍾、小時、天,月份和年份,如果您想在控制台中嘗試他的工作,但我想在 p 標簽中顯示,謝謝您幫助我:D

<p id="display"></p>
      

const SECONDS_IN_ONE_MINUTE = 60;
const SECONDS_IN_HOUR = 60 * SECONDS_IN_ONE_MINUTE; // 3600;
const SECONDS_IN_ONE_DAY = 24 * SECONDS_IN_HOUR; // 86400;
const SECONDS_IN_ONE_MONTH = 30.439814814814813 * SECONDS_IN_ONE_DAY; // 2630000
const SECONDS_IN_ONE_YEAR = 12 * SECONDS_IN_ONE_MONTH; // 31536000
function fromSeconds(value) {
    const seconds = Number.parseInt(String(value), 10);
    if (Number.isNaN(seconds)) {
        throw new TypeError('Invalid value to convert');
    }
    return {
        toMinutes: () => parseSecondsToMinutes(seconds),
        toHours: () => parseSecondsToHours(seconds),
        toDays: () => parseSecondsToDays(seconds),
        toMonths: () => parseSecondsToMonths(seconds),
        toYears: () => parseSecondsToYears(seconds),
    };
}

function parseSecondsToMinutes(s) {
    const minutes = Math.floor(s / SECONDS_IN_ONE_MINUTE);
    const seconds = Math.floor(s % SECONDS_IN_ONE_MINUTE);
    return {
        minutes,
        seconds,
    };
}
function parseSecondsToHours(s) {
    const hours = Math.floor(s / SECONDS_IN_HOUR);
    const minutes = Math.floor(s % SECONDS_IN_HOUR / SECONDS_IN_ONE_MINUTE);
    const seconds = Math.floor(s % SECONDS_IN_ONE_MINUTE);
    return {
        hours,
        minutes,
        seconds,
    };
}
function parseSecondsToDays(s) {
    const days = Math.floor(s / SECONDS_IN_ONE_DAY);
    const hours = Math.floor(s % SECONDS_IN_ONE_DAY / SECONDS_IN_HOUR);
    const minutes = Math.floor(s % SECONDS_IN_HOUR / SECONDS_IN_ONE_MINUTE);
    const seconds = Math.floor(s % SECONDS_IN_ONE_MINUTE);
    return {
        days,
        hours,
        minutes,
        seconds,
    };
}
function parseSecondsToMonths(s) {
    const months = Math.floor(s / SECONDS_IN_ONE_MONTH);
    const days = Math.floor(s % SECONDS_IN_ONE_MONTH / SECONDS_IN_ONE_DAY);
    const hours = Math.floor(s % SECONDS_IN_ONE_DAY / SECONDS_IN_HOUR);
    const minutes = Math.floor(s % SECONDS_IN_HOUR / SECONDS_IN_ONE_MINUTE);
    const seconds = Math.floor(s % SECONDS_IN_ONE_MINUTE);
    return {
        months,
        days,
        hours,
        minutes,
        seconds,
    };
}
function parseSecondsToYears(s) {
    const years = Math.floor(s / SECONDS_IN_ONE_YEAR);
    const months = Math.floor(s % SECONDS_IN_ONE_YEAR / SECONDS_IN_ONE_MONTH);
    const days = Math.floor(s % SECONDS_IN_ONE_MONTH / SECONDS_IN_ONE_DAY);
    const hours = Math.floor(s % SECONDS_IN_ONE_DAY / SECONDS_IN_HOUR);
    const minutes = Math.floor(s % SECONDS_IN_HOUR / SECONDS_IN_ONE_MINUTE);
    const seconds = Math.floor(s % SECONDS_IN_ONE_MINUTE);

    if (years === 0 && months === 0 && days === 0 && hours === 0 && minutes === 0) {
        return {
            seconds,
        };
    }
    else if (years === 0 && months === 0 && days === 0 && hours === 0) {
        return {
            minutes,
            seconds,
        };
    }
    else if (years === 0 && months === 0 && days === 0) {
        return {
            hours,
            minutes,
            seconds,
        };
    }
    else if (years === 0 && months === 0) {
        return {
            days,
            hours,
            minutes,
            seconds,
        };
    }
    else if (years === 0) {
        return {
            months,
            days,
            hours,
            minutes,
            seconds,
        };
    }
    else {
        return {
            years,
            months,
            days,
            hours,
            minutes,
            seconds,
        };
    }

}

//seconds
const result = fromSeconds(10000);

result.toMinutes();
// => { minutes: 525061, seconds: 1 }
result.toHours();
// => { hours: 8751, minutes: 1, seconds: 1 }
result.toDays();
// => { days: 364, hours: 15, minutes: 1, seconds: 1 }
result.toMonths();
// => { months: 11, days: 29, hours: 15, minutes: 1, seconds: 1 }
result.toYears();
// => { years: 0, months: 11, days: 29, hours: 15, minutes: 1, seconds:1 }


//console.log(result.toYears());

document.getElementById("display").innerHTML = result.toYears();

您正在嘗試 output 和 object,嘗試先將其解析為字符串,例如:

 const SECONDS_IN_ONE_MINUTE = 60; const SECONDS_IN_HOUR = 60 * SECONDS_IN_ONE_MINUTE; // 3600; const SECONDS_IN_ONE_DAY = 24 * SECONDS_IN_HOUR; // 86400; const SECONDS_IN_ONE_MONTH = 30.439814814814813 * SECONDS_IN_ONE_DAY; // 2630000 const SECONDS_IN_ONE_YEAR = 12 * SECONDS_IN_ONE_MONTH; // 31536000 function fromSeconds(value) { const seconds = Number.parseInt(String(value), 10); if (Number.isNaN(seconds)) { throw new TypeError('Invalid value to convert'); } return { toMinutes: () => parseSecondsToMinutes(seconds), toHours: () => parseSecondsToHours(seconds), toDays: () => parseSecondsToDays(seconds), toMonths: () => parseSecondsToMonths(seconds), toYears: () => parseSecondsToYears(seconds), }; } function parseSecondsToMinutes(s) { const minutes = Math.floor(s / SECONDS_IN_ONE_MINUTE); const seconds = Math.floor(s % SECONDS_IN_ONE_MINUTE); return { minutes, seconds, }; } function parseSecondsToHours(s) { const hours = Math.floor(s / SECONDS_IN_HOUR); const minutes = Math.floor(s % SECONDS_IN_HOUR / SECONDS_IN_ONE_MINUTE); const seconds = Math.floor(s % SECONDS_IN_ONE_MINUTE); return { hours, minutes, seconds, }; } function parseSecondsToDays(s) { const days = Math.floor(s / SECONDS_IN_ONE_DAY); const hours = Math.floor(s % SECONDS_IN_ONE_DAY / SECONDS_IN_HOUR); const minutes = Math.floor(s % SECONDS_IN_HOUR / SECONDS_IN_ONE_MINUTE); const seconds = Math.floor(s % SECONDS_IN_ONE_MINUTE); return { days, hours, minutes, seconds, }; } function parseSecondsToMonths(s) { const months = Math.floor(s / SECONDS_IN_ONE_MONTH); const days = Math.floor(s % SECONDS_IN_ONE_MONTH / SECONDS_IN_ONE_DAY); const hours = Math.floor(s % SECONDS_IN_ONE_DAY / SECONDS_IN_HOUR); const minutes = Math.floor(s % SECONDS_IN_HOUR / SECONDS_IN_ONE_MINUTE); const seconds = Math.floor(s % SECONDS_IN_ONE_MINUTE); return { months, days, hours, minutes, seconds, }; } function parseSecondsToYears(s) { const years = Math.floor(s / SECONDS_IN_ONE_YEAR); const months = Math.floor(s % SECONDS_IN_ONE_YEAR / SECONDS_IN_ONE_MONTH); const days = Math.floor(s % SECONDS_IN_ONE_MONTH / SECONDS_IN_ONE_DAY); const hours = Math.floor(s % SECONDS_IN_ONE_DAY / SECONDS_IN_HOUR); const minutes = Math.floor(s % SECONDS_IN_HOUR / SECONDS_IN_ONE_MINUTE); const seconds = Math.floor(s % SECONDS_IN_ONE_MINUTE); if (years === 0 && months === 0 && days === 0 && hours === 0 && minutes === 0) { return { seconds, }; } else if (years === 0 && months === 0 && days === 0 && hours === 0) { return { minutes, seconds, }; } else if (years === 0 && months === 0 && days === 0) { return { hours, minutes, seconds, }; } else if (years === 0 && months === 0) { return { days, hours, minutes, seconds, }; } else if (years === 0) { return { months, days, hours, minutes, seconds, }; } else { return { years, months, days, hours, minutes, seconds, }; } } //seconds const result = fromSeconds(10000); result.toMinutes(); // => { minutes: 525061, seconds: 1 } result.toHours(); // => { hours: 8751, minutes: 1, seconds: 1 } result.toDays(); // => { days: 364, hours: 15, minutes: 1, seconds: 1 } result.toMonths(); // => { months: 11, days: 29, hours: 15, minutes: 1, seconds: 1 } result.toYears(); // => { years: 0, months: 11, days: 29, hours: 15, minutes: 1, seconds:1 } //console.log(result.toYears()); //document.getElementById("display").innerHTML = result.toYears().hours + ":" + result.toYears().minutes + ":" + result.toYears().seconds; document.getElementById("display").innerHTML = `${result.toYears().hours || "00"}:${result.toYears().minutes || ""}:${result.toYears().seconds || "00"}`;
 <div id="display"></div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM