将div添加到以html显示的日期,并将日期和月份分开

时间:2018-11-14 09:50:50

标签: javascript jquery html5 jquery-append

我有一些日期列表,我想在日期中添加div,并将日期和月份分隔到另一个div。

<div class="campaign">30/11/2016 - 
  <a href="#" target="_blank">Dummy Text</a>
</div>

我要选择div并将其添加到日期。我也想分开这样的日子和月份。任何帮助,提前感谢。

<div class="campaign"> 
  <div class="date-wrapper">
    <div class="date">30</div>
    <div class="month">Nov</div>
    <div class="year">2016</div>
  </div> 
  <a href="#" target="_blank">Dummy Text</a>
</div>

我尝试了 append方法,但是我无法选择日期并将其存储在变量中。

4 个答案:

答案 0 :(得分:2)

您可以使用.prepend( function )在元素的第一个元素中插入新的HTML内容。

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];

$(".campaign").prepend(function(){
  var dates = $(this).contents()[0].nodeValue.trim().match(/\d+/g);
  $(this).contents()[0].nodeValue = "";
  return '\
    <div class="date-wrapper">\
    <div class="date">'+dates[1]+'</div>\
    <div class="month">'+monthNames[+dates[0]-1]+'</div>\
    <div class="year">'+dates[2]+'</div></div> \
    </div>';
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="campaign">11/30/2016 - 
  <a href="#" target="_blank">Dummy Text</a>
</div>

答案 1 :(得分:0)

检查它是否对您有用?

$('#convert').click(function(){
  let date = $('.campaign').text().split('/');
  
  let dateDiv ='<div class="date">'+date[1]+'</div>';
  let monthDiv = '<div class="month">'+date[0]+'</div>';
  let yearDiv = '<div class="year">'+date[2]+'</div></div>';
  
  $('.campaign').html(dateDiv + monthDiv + yearDiv)
  

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="campaign">11/30/2016 - 
<a href="#" target="_blank">Dummy Text</a>
</div>

<button id="convert">Convert</button>

答案 2 :(得分:0)

晚点参加聚会。我的解决方案使用date parsing提取日期结果。在这种情况下,Prepending div要简单得多。

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];

var campaign = $($('.campaign')[0]);
var dateString = campaign.text().split('-')[0];
var date = new Date(Date.parse(dateString));

campaign.contents()[0].nodeValue = "";
campaign.prepend('<div class="year">'+date.getFullYear()+'</div>');
campaign.prepend('<div class="month">'+months[date.getMonth()]+'</div>');
campaign.prepend('<div class="date">'+date.getDate()+'</div>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="campaign">11/30/2016 - 
<a href="#" target="_blank">Dummy Text</a>
</div>

答案 3 :(得分:0)

尝试一下。

$('.campaign').each(function(){

	var dateStr = $(this).text().split('/'),
		dateDD = dateStr[0],
		dateMM = dateStr[1],
		dateYY = dateStr[2];

	function GetMonthName(monthNumber) {

		var months = ['Jan', 'Feb', 'Mar', 'Avr', 'Mai', 'Juin', 'Juil', 'Août', 'Sept', 'Oct', 'Nov', 'Dec'];
		return months[monthNumber - 1];

	}

	var newDate = '<div class="campaign1">'+'<div class="date-wrapper">'+'<div class="date">'+dateStr[0]+'</div>'+ '  <div class="month">'+GetMonthName(dateMM) + '</div> ' + '<div class="year">'+dateStr[2]+'</div>'+'</div>'+'<a href="#" target="_blank">Dummy Text</a>'+'</div>';
	$('.test').append(newDate);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="campaign">30/11/2016</div>  
<a href="#" target="_blank">Dummy Text</a>


<div class="test"></test>