L'appel d'une fonction définie par l'utilisateur dans jQuery
-
22-09-2019 - |
Question
Je suis en train d'appeler une fonction définie par l'utilisateur dans jQuery:
$(document).ready(function() {
$('#btnSun').click(function() {
myFunction();
});
$.fn.myFunction = function() {
alert('hi');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnSun">Say hello!</button>
J'ai essayé les éléments suivants ainsi:
$(document).ready(function() {
$('#btnSun').click(function() {
myFunction();
});
});
function myFunction() {
alert('hi');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnSun">Say hello!</button>
Il ne semble pas fonctionner! Toute idée où je me trompe?
La solution
Si vous voulez appeler une fonction normale via un événement jQuery, vous pouvez le faire comme ceci:
$(document).ready(function() {
$('#btnSun').click(myFunction);
});
function myFunction() {
alert('hi');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnSun">Say hello!</button>
Autres conseils
Juste essayer. Il fonctionne toujours.
$(document).ready(function() {
$('#btnSun').click(function() {
$.fn.myFunction();
});
$.fn.myFunction = function() {
alert('hi');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnSun">Say hello!</button>
Ils sont appelés plugins , a commenté comme Jaboc. Pour donner un sens, la fonction plug-in doit faire quelque chose avec l'élément auquel il est appelé par. Considérez ce qui suit:
jQuery.fn.make_me_red = function() {
return this.each(function() {
this.style.color = 'red';
});
};
$('a').make_me_red();
Ce qui suit est la bonne méthode
$(document).ready(function() {
$('#btnSun').click(function(){
$(this).myFunction();
});
$.fn.myFunction = function() {
alert('hi');
}
});
Essayer cette $('div').myFunction();
Cela devrait fonctionner
$(document).ready(function() {
$('#btnSun').click(function(){
myFunction();
});
function myFunction()
{
alert('hi');
}
jQuery.fn.make_me_red = function() {
return this.each(function() {
this.style.color = 'red';
});
};
$('a').make_me_red() // - instead of this you can use $(this).make_me_red() instead for better readability.
$(document).ready(function() {
$('#btnSun').click(function(){
myFunction();
});
$.fn.myFunction = function() {
alert('hi');
};
});
Put '; 'Après définition de la fonction ...
jQuery.fn.make_me_red = function() {
alert($(this).attr('id'));
$(this).siblings("#hello").toggle();
}
$("#user_button").click(function(){
//$(this).siblings(".hello").make_me_red();
$(this).make_me_red();
$(this).addClass("active");
});
Déclaration de fonction et rappel jQuery .
function hello(){
console.log("hello")
}
$('#event-on-keyup').keyup(function(){
hello()
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<input type="text" id="event-on-keyup">
jQuery.fn.clear = function()
{
var $form = $(this);
$form.find('input:text, input:password, input:file, textarea').val('');
$form.find('select option:selected').removeAttr('selected');
$form.find('input:checkbox, input:radio').removeAttr('checked');
return this;
};
$('#my-form').clear();