【问题标题】:Problem with datepicker template and my custom javascript and cssdatepicker 模板和我的自定义 javascript 和 css 的问题
【发布时间】:2020-11-03 12:56:32
【问题描述】:

我试图在我的网页上包含一个漂亮的日期输入字段。我已经完成了输入字段的样式。它有一个浮动标签,由 'onfocus' 和 'onblur' 事件使用 jquery 方法处理。这一切都很好。我已经展示了图片。

但是当我使用来自该站点 https://gijgo.com/datepicker/ 的日期选择器模板时出现问题。日期选择器工作也没有问题。它能够在我的输入字段中选择日期。但是我的浮动标签出现了问题。标签不会在单击输入框时向上翻转,当我选择一个日期时,它也会与标签重叠,如图所示。

// Datepicker from website https://gijgo.com/datepicker/.
$('.datepicker').datepicker({
  showRightIcon: false
});


// Floating label onfocus and onblur handling.
function floatOnFocus(evt) {
  $(evt).parent().find('.place').css("font-size", "88%");
  $(evt).parent().find('.place').css("top", "-11px");
  $(evt).parent().find('.place').css("color", "#1b75cf");
  $(evt).parent().find('.place').css("background-color", "white");

}

function makeInpFocus(evt) {
  $(evt).parent().find('#inpbox').focus();
}

function floatOnBlur(evt) {
  if ($(evt).val() == "") {
    $(evt).parent().find('.place').css("font-size", "100%");
    $(evt).parent().find('.place').css("top", "7px");
    $(evt).parent().find('.place').css("color", "grey");
    $(evt).parent().find('.place').css("background-color", "transparent");
  }
}
// Floating label onfocus and onblur handling end.
.maindiv {
  position: relative;
}

.place {
  position: absolute;
  left: 9px;
  top: 7px;
  height: 56%;
  font-size: 100%;
  color: grey;
  transition-duration: 0.2s;
  white-space: nowrap;
}
<!-- Input field without datepicker template, Custom CSS and jquery functions are working.-->
<div class="maindiv">
  <span class="place" onclick="makeInpFocus(this)">Test Date</span>
  <input type="text" name="testDate" id="inpbox" onfocus="floatOnFocus(this)" onblur="floatOnBlur(this)" placeholder="">
</div>


<!-- Input field with datepicker template, Custom CSS and jquery functions are not working.-->
<div class="maindiv">
  <span class="place" onclick="makeInpFocus(this)">Test Date</span>
  <input class="datepicker" type="text" name="testDate" id="inpbox" onfocus="floatOnFocus(this)" onblur="floatOnBlur(this)" placeholder="">
</div>
<br>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://unpkg.com/gijgo@1.9.13/js/gijgo.min.js" type="text/javascript"></script>
<link href="https://unpkg.com/gijgo@1.9.13/css/gijgo.min.css" rel="stylesheet" type="text/css" />

最初它看起来像这样,带有红色警告框,这也是一个问题。

选择日期后是这样的。

您可以看到标签和选定的日期重叠。 这意味着这个模板不允许我的 jquery 和 css 工作。我只想知道如何使这个模板与我的自定义样式和事件函数一起工作。或者,如果您知道任何其他可以为我提供浮动标签样式的日期选择器模板,请告诉我。

【问题讨论】:

  • 截图没有帮助。您需要发送与您的问题相关的源代码或工作示例的链接。但我猜您使用的日期选择器库中的某些内容会覆盖您的 CSS 定义或 JS 代码。只需在浏览器的开发者工具 (F12) 中检查 CSS 样式并调试 JS
  • @Tacud 实际上,它只是我整个网页的一小部分,其中还有许多其他内容。如果我找到整个网页代码,那么找到问题将是一项繁重的任务。但是等一下,我将这部分的代码分开在不同的文件中。完成后我会更新。
  • @Tacud 我已经重新发布了标题为“日期选择器模板和我的自定义 jquery 和 css 的问题”的问题,没有截图,只有源代码。请看一下

标签: javascript html jquery css datepicker


【解决方案1】:

问题是日期选择器在输入周围包裹了一个额外的div,因此调用parent() 得到的是div 而不是span 的父级。

添加另一个对parent() 的调用,您将获得正确的元素:

  $(evt).parent().parent().find('.place').css("font-size", "88%");
  $(evt).parent().parent().find('.place').css("top", "-11px");
  $(evt).parent().parent().find('.place').css("color", "#1b75cf");
  $(evt).parent().parent().find('.place').css("background-color", "white");

【讨论】:

  • 但是还有一个问题...为什么输入框从一开始就显示红色警告边框,即使没有按下提交按钮?
  • 那一定是因为你没有显示代码,因为你的 sn-p 没有变红。
  • 好的...让我看看
  • @Avi 你应该发布你的答案,以防其他人有同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
相关资源
最近更新 更多