问题
I want jQuery and slimpicker work together, what should I do?:
Sample from http://jsfiddle.net
<html>
<head>
<link rel="stylesheet" href="http://www.styledisplay.com/scripts/slimpicker/pagestyle.css" media="screen, projection" />
<link rel="stylesheet" href="http://www.styledisplay.com/scripts/slimpicker/slimpicker.css" media="screen, projection" />
<script src="http://www.styledisplay.com/scripts/slimpicker/mootools-1.2.4-core-yc.js"></script>
<script src="http://www.styledisplay.com/scripts/slimpicker/mootools-1.2.4.4-more-yc.js"></script>
<script src="http://www.styledisplay.com/scripts/slimpicker/slimpicker.js"></script>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#button").click(function () {
alert("I'm inside jQuery function!");
});
});
</script>
</head>
<body>
<div class="container">
<h1>SlimPicker</h1>
<div class="intro">
<p>Date Picker that works in IFrames. Also allows for keyboard navigation.</p>
</div>
<p>
<label for="new_day">default calendar</label>
<input id="new_day" name="new_day" type="text" class="slimpicker" autocomplete="off" value="" />
</p>
<p>
<label for="birthday">calendar with options</label>
<input id="birthday" name="birthday" type="text" class="slimpicker" autocomplete="off" alt="{
dayChars:3,
dayNames:['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
daysInMonth:[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
format:'yyyy-mm-dd',
monthNames:['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
startDay:1,
yearOrder:'desc',
yearRange:90,
yearStart:2007
}" value="1980-03-13" />
</p>
<p>
This button uses <strong>jquery-1.7.2.min.js plugin</strong>
<input type="button" id="button" value="Click me" style="{text-align:center}"/>
<br />
To activate calendar, remove this reference from the code <br />
<strong>http://code.jquery.com/jquery-1.7.2.min.js</strong>
</p>
</div>
<script>
$$('input.slimpicker').each( function(el){
var picker = new SlimPicker(el);
});
</script>
</body>
</html>
回答1:
you can get it to work easily. The problem here is that SlimPicker is old and does not adhere to mootools codex - i.e. it still uses $ in preference to document.id.
you can fix it by creating a closure around the class: http://jsfiddle.net/dimitar/ZRGAd/9/
in code it's like:
(function($) {
var SlimPicker = this.SlimPicker = new Class({
method: function() {
console.log($ == jQuery); // false
}
});
}(document.id));
console.log($ == jQuery); // true
or, search and replace all $() references in the class with document.id
回答2:
If I'm not mistaken. Both mootools and jQuery uses $ for shorthand for their main functions. Hence you get a collision.
I would look in to jQuery noConflict.
Also I tried removing all the jQuery code from your jsFiddle and it still throws a lot of errors. Don't know mootools, but try to fixes the error then add jQuery with the noConflict.
来源:https://stackoverflow.com/questions/10103544/slimpicker-doesnt-work-with-jquery-plugin