简体   繁体   English

当事件被另一个函数的元素调用时,JavaScript函数不起作用

[英]Javascript function not working when the event is called by element from another function

I've downloaded a calendar script, that when the input element is click a calendar will popup. 我已经下载了一个日历脚本,当单击输入元素时,将弹出一个日历。

<script language="JavaScript">
  $(function() {
        $( ".datepicker" ).datepicker();                
  });   
</script>

<input class='datepicker' />

The above code works fine. 上面的代码工作正常。

But when the element is coming from a javascript... 但是当元素来自JavaScript时...

<script language="JavaScript">
function showElement()
{
    $('#mydiv').html("<input class='datepicker' />");

}
</script>

<div id='mydiv'></div>

The calendar don't shows up as the input is clicked. 单击输入后,日历不会显示。 I think there is no error in the downloaded script. 我认为下载的脚本没有错误。 I think I need to do something to make this calendar appears even if the element is created by a javascript. 我认为即使元素是由javascript创建的,我也需要做些使该日历显示的操作。 What should I modify? 我应该修改什么? Thanks! 谢谢!

Change your function to: 将功能更改为:

function showElement()
{
    $('#mydiv').html("<input class='datepicker' />");
    $( ".datepicker" ).datepicker();

}

Or even better, do the following: 甚至更好,请执行以下操作:

function showDatePicker(selector) {
    $(selector).datepicker();
}

function showElement()
{
    $('#mydiv').html("<input class='datepicker' />");
    showDatePicker(".datepicker");
}

This way you can show the date picker whenever you want by calling the showDatePicker() function for any element that matches the selector you pass as a parameter to the function. 这样,您可以通过对与作为参数传递给函数的选择器匹配的任何元素调用showDatePicker()函数,随时显示日期选择器。 Of course in your case you would only want to call it after you have called the showElement() function 当然,在您的情况下,您只想在调用showElement()函数之后才调用它

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

相关问题 当其他函数创建的元素调用事件时,.click函数不起作用 - .click function not working when the event is called by element created by another function 调用事件时函数不起作用 - Function not working when event is called 为什么 Bootstrap 4 轮播暂停功能在从轮播内元素的事件中被调用时不起作用? - Why is Bootstrap 4 carousel pause function not working when it's getting called from the event of an element inside of the carousel? javascript中的函数在调用时不起作用 - function in javascript not working when called 从输入图像onclick事件调用时,Javascript函数未定义 - Javascript function undefined when called from input image onclick event 从另一个对象中调用时,这是javascript函数 - this of a javascript function when called from within another object 为什么我的 function 在自己调用时工作,但当我与另一个 function 一起调用时却不行? (初学者javascript问题) - Why is my function working when called by itself but not when I call it with another function? (Beginner javascript question) 单击函数无法像从函数调用时那样工作 - Click function not working as when called from a function 在javascript中调用另一个函数时值不变 - Value unchanged when another function is called in javascript 通过另一个函数调用时,clearTimeout()不起作用 - clearTimeout() is not working when called via another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM