function Prototype in jQuery
I have two files - prototype.js and jquery.js
In prototype.js have:
function check(www) {
//....
}
in jquery.js have:
jQuery('#aaa').click(function() {
var bbb = $this.val();
//-----
check(www);
//--------
});
How can I corre开发者_Go百科ctly use check?
If it were really declared as shown, you could just call it as you've shown. It doesn't matter that it's defined in one file and then used in another; once the files are loaded, they all exist in the same execution environment. But I don't recall Prototype having a check
function at global scope.
Note that if you're using Prototype and jQuery on the same page, you have to tell jQuery to not use the $
symbol:
jQuery.noConflict();
Then you can just intersperse use of Prototype and jQuery all you want. Example:
<script src='prototype.js'></script>
<script src='jquery.js'></script>
<script>
jQuery.noConflict();
jQuery(function() {
// Get a jQuery instance containing elements with class "foo"
var foo = jQuery(".foo");
// Retrieve the first matching element, use Prototype's `$()` to extend it
// (automatic on some browsers, not on IE), then call Prototype's `update` function.
$(foo[0]).update("Hi there");
});
</script>
As you can see, you can intersperse the two if you like. I'm not saying it's a good idea, but sometimes it's necessary, particularly when you're incrementally transitioning a page or site from one library to another.
If you're used to writing jQuery code with the $
symbol, you can still do that for entire sections of the code if you like. Here's a fairly common idiom:
(function($) {
// Within this function, $ = jQuery
....
})(jQuery);
That shadows the $
symbol within that function (by making it an argument), and passes jQuery
in for that argument. Or you can be more explicit:
(function() {
var $ = jQuery; // Within this function, $ = jQuery
....
})();
That does much the same thing, but more obviously.
精彩评论