简体   繁体   English

如何在我的内部显示我的 javascript<p> 标签使它成为一个单行句子?</p>

[英]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.

相关问题 我怎样才能让我的 Javascript 代码 select 在页面上显示所有“b”标签,而不是只有第一个? - How can I make my Javascript code select all 'b' tags on a page instead of only the first one? 我如何让javascript重新排序我的div标签? - How could I make javascript reorder my div tags? 我如何获得除“ p”以外的所有“ p” <p> 标签-javascript - How can I get all “p” except <p> tags -javascript 如何在Ajax请求中包含Javascript脚本标签? - How can I include Javascript script tags in my Ajax request? 如何让我的代码等到我从服务器获得处理过的图像以在 Javascript 中显示 - How can I make my code wait until I got processed image from the server to display in Javascript 我该如何添加 <td> 用JavaScript标记我的html表格? - How can I add <td> tags to my html table with JavaScript? WordPress 添加<p>标签到我的 javascript - Wordpress add <p> tags to my javascript 如何使Magento中的JS脚本调用显示在一行上? - How can I make my JS script calls in Magento appear on one line? 如何让输出显示在一行中没有空格? - How can I make my output appear all on one line with no spaces? 如何在Javascript中更改其显示属性时使元素淡入和淡出? - How can I make my elements fade in and out as their display attributes are changed in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM