简体   繁体   English

如何从HTML中的外部文件使用Jquery对象?

[英]How to use an Jquery object from external file in HTML?

I am relatively new to frond-end. 我对新手比较陌生。

I was wondering how I can use an jquery object returned from external link. 我想知道如何使用从外部链接返回的jquery对象。

For example, a js file on this location( https://github.com/jquery/jquery-ui/blob/master/ui/i18n/datepicker-en-NZ.js ) returns 'datepicker.regional[ "en-NZ" ];' 例如,此位置上的js文件( https://github.com/jquery/jquery-ui/blob/master/ui/i18n/datepicker-en-NZ.js )返回'datepicker.regional [“ en-NZ “];' object like below 如下对象

( function( factory ) {
    if ( typeof define === "function" && define.amd ) {

        // AMD. Register as an anonymous module.
        define( [ "../widgets/datepicker" ], factory );
    } else {

        // Browser globals
        factory( jQuery.datepicker );
    }
}( function( datepicker ) {

datepicker.regional[ "en-NZ" ] = {
    closeText: "Done",
    prevText: "Prev",
    nextText: "Next",};
datepicker.setDefaults( datepicker.regional[ "en-NZ" ] );

return datepicker.regional[ "en-NZ" ];

I import this js file to main html file and embed it in head tag. 我将此js文件导入到主要的html文件中,并将其嵌入head标签中。

<script src="path/datepicker-en-NZ.js"></script>

Now I need use the returned object which is datepicker.regional["en-NZ"] . 现在,我需要使用返回的对象,它是datepicker.regional [“ en-NZ”] How can I use this object in main html file? 如何在主要html文件中使用此对象?

Check example code in project : 检查项目中的示例代码:

  1. I would suggest you add Bootstrap.js in your html 我建议您在HTML中添加Bootstrap.js
  2. You need to add input control in your html file ie id = myDate 您需要在HTML文件中添加输入控件,即id = myDate
  3. Bind it with datepicker 与datepicker绑定
$( "#myDate" ).datepicker(); $(“ #myDate”).datepicker();

I've found a demo on the jQueryUI website that provides some insight into how to use their datepicker: http://jqueryui.com/datepicker/#localization 我在jQueryUI网站上找到了一个演示,该演示提供了有关如何使用其日期选择器的一些见解: http : //jqueryui.com/datepicker/#localization

I believe the Javascript in the source code provided is the important bit: 我相信所提供的源代码中的Javascript是重要的一点:

  $( "#datepicker" ).datepicker( $.datepicker.regional[ "fr" ] );

My understanding is that you need to create a Datepicker object and bind it to an <input> using its ID. 我的理解是,您需要创建一个Datepicker对象,并使用其ID将其绑定到<input>

<input type="text" id="datepicker" />

OK, I found the new(for me) concept. 好的,我发现了新的(对我而言)概念。

This is called Factory function. 这称为工厂功能。

The definition of Factory function is A function that returns an object. Factory函数的定义是一个返回对象的函数。

In my case, I was trying to use an object created from factory function which was datepicker.regional[ "en-NZ" ] . 就我而言,我试图使用从工厂函数创建的对象datepicker.regional [“ en-NZ”]

So I could call and use this object in main html file by adding $. 因此,我可以通过添加$来在主html文件中调用和使用此对象 before the name of object. 在对象名称之前。

<script>
$(document).ready(function(){
    $('#datepicker').datepicker($.datepicker.regional[ "zh-CN" ] );
});
</script>

And it was also important to add document.ready part unless it won't start kicking in. 除非它不会开始起作用,否则添加document.ready部分也很重要。

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

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