最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to underline string via JavaScript? - Stack Overflow

programmeradmin0浏览0评论

I am working on a project involving Javascript and I need a string to be underlined. The code has the word "Date" in front of the current date (represented by the variable now). The current date needs to be underlined, not the word "Date".

This is the code I am using:

var now = new Date();
document.getElementById('date').innerHTML = "Date " + (now.getMonth()+1)+ "/"+ now.getDate()+ "/"+ now.getFullYear();

I am working on a project involving Javascript and I need a string to be underlined. The code has the word "Date" in front of the current date (represented by the variable now). The current date needs to be underlined, not the word "Date".

This is the code I am using:

var now = new Date();
document.getElementById('date').innerHTML = "Date " + (now.getMonth()+1)+ "/"+ now.getDate()+ "/"+ now.getFullYear();

How can I do this?

Share Improve this question edited Jun 22, 2017 at 20:38 Christian Sirolli asked Dec 24, 2016 at 16:46 Christian SirolliChristian Sirolli 2601 gold badge6 silver badges19 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

When something with the ID of "date" is found...

Use CSS for presentation:

#date {text-decoration: underline;}

Snippet

var now = new Date();
document.getElementById('date').innerHTML = "Date <span>" + (now.getMonth()+1)+ "/"+ now.getDate()+ "/"+ now.getFullYear() + "</span>";
#date span {
  text-decoration: underline;
}
<div id="date"></div>

Also, please note, there should not be any duplication of ids. So if there are multiple date elements in the same page, use class instead and style this way:

.date {text-decoration: underline;}

Preview

发布评论

评论列表(0)

  1. 暂无评论