根据不包括年份的实际日期显示/隐藏div

时间:2016-02-09 16:25:38

标签: javascript html5 css3

我有7/8个div,当他们不相关时我需要隐藏它们。我已经为此工作了一个星期而且我很难过。 psudocode:

$parent.find('input[name="' + name + '"][value="' + value + '"]').click();

已经建议了几种方法来做到这一点,但不是它应该表现的方式。我需要它在2016,2017等行为方式。

随时建议使用任何库或预制脚本更好地执行此操作。

由于

2 个答案:

答案 0 :(得分:0)

这样的事情怎么样?

http://codepen.io/anon/pen/xZQyLr

HTML

<!-- the dates are in "MM-DD" format; always 5 characters altogether -->
<div data-visible-from="01-09" data-visible-to="02-15">Hi!</div>
<div data-visible-from="02-10" data-visible-to="03-15">Hi too!</div>

的JavaScript

// The current month and year, sliced from the full ISO8601 date string,
// i.e. "02-16" from "2016-02-16T......"
var md = (new Date()).toISOString().substring(5, 10);
// Find all elements with the visible-from or visible-to attributes,
// turn them into an array with [].slice.call and then iterate over them
// with the regular array forEach...
[].slice.call(document.querySelectorAll("[data-visible-from],[data-visible-to]")).forEach(function(el) {
  // Nab the visible-from date, or if it's not available, then the smallest
  // possible date per year (1 Jan)
  var from = el.dataset.visibleFrom || "01-01";
  // Ditto, with 31 Dec being the fallback.
  var to = el.dataset.visibleTo || "12-31";
  // Then simply set the element's `display` to none if today's not the day.
  el.style.display = (from <= md && md <= to ? "" : "none");
});

答案 1 :(得分:0)

希望这个帮助

<html><body>

<div style="display:none" showbefore = a showafter = b>advert for valentines</div>
<div  style="display:none" showbefore = a showafter = c>advert for mothers day</div>
<div style="display:none" showbefore = d showafter = f>advert for easter sunday</div>
<div style="display:none" showbefore = h showafter = x>advert for christmas. </div>

<script>
  var date = new Date(),
  day = date.getDate(),
  month = date.getMonth();

  if (day == 14 && month ==2) {
    document.getElementsByTagName("div")[0].style.display = "block";
  }
  else if () {
    //...
  }

</script>
</body>
</html>