如何避免使用document.write

huangapple go评论64阅读模式
英文:

How to avoid document.write

问题

以下是代码部分的翻译:

function convertTimestamp(timestamp) {
    var currentTime = new Date();
    var convertedTimestamp = new Date(timestamp);
    var offset = -new Date().getTimezoneOffset();
    convertedTimestamp = new Date(convertedTimestamp.getTime() + offset * 60 * 1000);
    var timeDifference = currentTime - convertedTimestamp;

    var seconds = Math.floor(timeDifference / 1000);
    var minutes = Math floor(seconds / 60);
    var hours = Math floor(minutes / 60);

    if (seconds < 60) {
        return seconds + ' ';
    } else if (minutes < 60) {
        return minutes + ' ';
    } else (hours < 24) {
        return hours + ' 小时';
    }
}

至于你提到的是否可以使用其他单词代替document.write,你可以尝试以下两种方法:

  1. 使用document.body.append
document.body.append(convertTimestamp("<data:post.timestamp/>"));
  1. 使用document.body.innerHTML
document.body.innerHTML = convertTimestamp("<data:post.timestamp/>");

这些代码片段可以替代原来的document.write,并将转换后的时间戳插入到文档中。

英文:

I've a script and I want to use another sentence instead of document.write()
as you can see at the last line
document.write(convertTimestamp("<data:post.timestamp/>"));

<script>
    function convertTimestamp(timestamp) {
    var currentTime = new Date();
    var convertedTimestamp = new Date(timestamp);
    var offset = -new Date().getTimezoneOffset();
    convertedTimestamp = new Date(convertedTimestamp.getTime() + offset * 60 * 1000);
    var timeDifference = currentTime - convertedTimestamp;

    var seconds = Math.floor(timeDifference / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);


    if (seconds < 60) {
    return seconds + ' seconds39;;
} else if (minutes < 60) {
    return minutes + ' mins ';
} else (hours < 24) {
    return hours + ' hours';
}
}
    document.write(convertTimestamp("<data:post.timestamp/>"));
</script>

Can I use other words instead of document.write?
I saw a recommendation to use
document.body.append
or
document.body.innerHTML
but I don't know how to apply it on my code.

答案1

得分: 1

以下是翻译好的部分:

创建元素:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let elementToAdd = document.createElement('p');
elementToAdd.innerHTML = `在这里添加您的表达式`;
document.body.appendChild(elementToAdd);
<!-- end snippet -->

向元素添加内容:

elementToAdd.innerHTML = convertTimestamp("<data:post.timestamp/>");

将该元素附加到 body:

document.body.appendChild(elementToAdd);

这将导致代码增加,但将有助于更好地扩展它,更容易被肉眼识别。

希望这对您有所帮助!

英文:

A better way and essentially a different way to do this is:

Create the element:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let elementToAdd = document.createElement(&#39;p&#39;);
elementToAdd.innerHTML = `Your expression here`;
document.body.appendChild(elementToAdd)

<!-- end snippet -->

Add content to the element:

elementToAdd.innerHTML = convertTimestamp(&amp;quot;&lt;data:post.timestamp/&gt;&amp;quot;)

Append that element to the body:

document.body.appendChild(elementToAdd)

This will result in increased code but it'll help you scale it better in future and is more obvious to the naked eye.

Hope this helps!

huangapple
  • 本文由 发表于 2023年2月23日 21:15:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定