简体   繁体   English

具有相同名称的JS多功能

[英]JS multiple function with the same name

I use different javascript function. 我使用了不同的javascript函数。 The problem is that they have the same name so they overwrite each other. 问题在于它们具有相同的名称,因此它们会相互覆盖。 I would like to put a different name but so far it didn't work. 我想换一个名字,但到目前为止没有用。

Here one of the function and how I use it: 这里是功能之一以及如何使用它:

<script type="text/javascript">     
    $(function() {
        $('#datepicker').multiDatesPicker({
            altField: '#date',
            dateFormat: "yy-mm-dd",
        });
</script>

<div id="datepicker"></div>

The other function start like this too $(function() {} and use the same way <div id="date"></div> 另一个函数也像$(function() {}并使用<div id="date"></div>

I try to put $(function name() but it didn't work. Do you have any idea? Thanks 我尝试放入$(function name()但是没有用。你有什么主意吗?

This is a call to $ (in your case, it's jQuery), with a single argument, which happens to be an anonymous function. 这是对$ (在您的情况下为jQuery)的调用,它带有一个参数,而该参数恰好是一个匿名函数。 It doesn't have a name, and having more than one occurrence of this pattern will not "overwrite" the previous: 它没有名称,并且多次出现此模式不会“覆盖”先前的模式:

$(function() {
    // ...
});

Passing a function to $ is just shorthand for $(document).ready() . 将函数传递给$只是$(document).ready()简写。 If you need more than one, you should be able to simply combine them: 如果您需要多个,则应该可以简单地将它们组合:

$(function() {
    // Initialise your date picker
    // Do some other stuff
});

that function is shorthand for $(document).ready() which is just a binding to the ready event of the document. 该函数是$(document).ready()的简写,只是对文档ready事件的绑定。 all that means is you can have as many of them as you want without overwriting eachother. 这意味着您可以根据需要拥有任意数量的资源,而不会彼此覆盖。

see the jsfiddle 看到jsfiddle

    $(function()
  {
      alert('func1');
  });
$(function()
  {
      alert('func2');
  });

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

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