[英]How can I display my javascript inside my <p> tags to make it a one line sentence?
I have a script that displays the date 2 weeks in the future.我有一个脚本显示未来 2 周的日期。 Everything works correctly, but I'm wanting to clean it up more and make it display in one sentence, rather than broken into two paragraphs.一切正常,但我想对其进行更多清理并使其在一个句子中显示,而不是分成两段。 I cannot for the life of me figure out how to do this.我一生都无法弄清楚如何做到这一点。 Any help will be greatly appreciated.任何帮助将不胜感激。
<script>
var twoWeeksForward = new moment().add(14, 'day');
document.write(twoWeeksForward.format('dddd, MMMM DD, YYYY'));
</script>
<p style="text-align:center"><i class="fas fa-tshirt"></i>Complete your order now and receive it by </p><span class="date"><p class ="twoWeeksForward">
As others said in comments, it's best not to use document.write()
.正如其他人在评论中所说,最好不要使用document.write()
。 But if you really must use it, you have to put the script inside the <p>
.但是如果你真的必须使用它,你必须把脚本放在<p>
里面。 When the page is loading, it writes to the current point in the HTML.当页面加载时,它会写入 HTML 中的当前点。
<p style="text-align:center"><i class="fas fa-tshirt"></i>Complete your order now and receive it by </p>
<span class="date">
<p class ="twoWeeksForward">
<script>
var twoWeeksForward = new moment().add(14, 'day');
document.write(twoWeeksForward.format('dddd, MMMM DD, YYYY'));
</script>
</p>
</span>
Try manipulating the innerText
of the paragraph you'd like to add the text to.尝试操作要添加文本的段落的innerText
。 For example, for a paragraph with id="my-p"
, you could use:例如,对于id="my-p"
的段落,您可以使用:
document.getElementById("my-p").innerText += twoWeeksForward.format('dddd, MMMM DD, YYYY');
This way you will not need a new element every time you update text with javascript.这样,每次使用 javascript 更新文本时都不需要新元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.