英文:
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
,你可以尝试以下两种方法:
- 使用
document.body.append
:
document.body.append(convertTimestamp("<data:post.timestamp/>"));
- 使用
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(&quot;<data:post.timestamp/>&quot;));
<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 &lt; 60) {
return seconds + &#39; seconds39;;
} else if (minutes &lt; 60) {
return minutes + &#39; mins &#39;;
} else (hours &lt; 24) {
return hours + &#39; hours&#39;;
}
}
document.write(convertTimestamp(&quot;<data:post.timestamp/>&quot;));
</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('p');
elementToAdd.innerHTML = `Your expression here`;
document.body.appendChild(elementToAdd)
<!-- end snippet -->
Add content to the element:
elementToAdd.innerHTML = convertTimestamp(&quot;<data:post.timestamp/>&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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论