简体   繁体   English

使用'innerHTML'不显示datepicker

[英]datepicker doesn't display by using 'innerHTML'


I am using datepicker from "Modernizr 2.6.2" and it is so useful. 我正在使用“Modernizr 2.6.2”中的datepicker,它非常有用。
It works so fine in this example code 它在这个示例代码中工作得很好

  <script>
$(function() {
    if (!Modernizr.inputtypes['date']) {
        $('input[type=date]').datepicker({
            dateFormat: 'yy-mm-dd'
        });
    }
});
</script>
    ...
<input class="datepicker input-usmall" type="date" id="startDate" name="startDate" data-date-format="yyyy-mm-dd" placeholder="click" autocomplete="off" form="addProblem">

However, I met one problem. 但是,我遇到了一个问题。 I found that it is not working in below example. 我发现它不适用于下面的例子。

<td ondblclick="modifySelector(this.id)" id="nof_{{ ownProblem.courseId }}_{{ ownProblem.problemId }}_{{ ownProblem.isAllInputCaseInOneFile }}">{{ ownProblem.isAllInputCaseInOneFile }}</td>`
<script>
function modifySelector(target){
    if(target.substring(0, 3)=="nof"){
        if( document.getElementById(target).innerHTML == "OneFile")
            document.getElementById(target).innerHTML = "MultipleFiles";
        else
             document.getElementById(target).innerHTML = "OneFile";
    }
    else if(target.substring(0, 9)=="startDate"){
        document.getElementById(target).innerHTML = '<input class="datepicker input-usmall" type="date" id="startDate" name="startDate" data-date-format="yyyy-mm-dd" placeholder="click" autocomplete="off" form="addProblem">;
    }
}

</script>

I guess, The difference is whether it has written directly or indirectly by using 'innerHTML' and I think it is about priority or something related with DOM. 我猜,不同之处在于它是使用'innerHTML'直接还是间接编写的,我认为它是关于优先级或与DOM相关的东西。 but I do not know how to solve it clearly. 但我不知道如何解决它。

Solved it. 解决了它。
I added 'onmousedown' event on 'input' tag. 我在'input'标签上添加了'onmousedown'事件。
Just like below example. 就像下面的例子一样。

<script>
if(target.substring(0, 9)=="startDate"){ 
    document.getElementById(target).innerHTML = '<input class="datepicker input-usmall" type="date" id="startDate" name="startDate" data-date-format="yyyy-mm-dd" placeholder="click" autocomplete="off" form="addProblem" onmousedown="datepick();">';
}    

// added here new function
function datepick() {
    if (!Modernizr.inputtypes['date']) {
        $('input[type=date]').datepicker({
            dateFormat: 'yy-mm-dd'
        });
    }
}
</script>

and it works now. 它现在有效。 Anyone knows another way to solve it, answer me. 任何人都知道另一种解决方法,回答我。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM