استدعاء دالة معرفة من قبل المستخدم في مسج
-
22-09-2019 - |
سؤال
أنا أحاول أن استدعاء دالة معرفة من قبل المستخدم في مسج:
$(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>
حاولت التالية فضلا:
$(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>
لا يبدو أن العمل!أي فكرة أين أنا على خطأ ؟
المحلول
إذا كنت ترغب في الاتصال بوظيفة عادية عبر حدث jQuery ، فيمكنك القيام بذلك على هذا النحو:
$(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>
نصائح أخرى
فقط جرب هذا. إنه يعمل دائمًا.
$(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>
يطلق عليهم الإضافات, ، كما علق جابوك. لما هو منطقي ، يجب أن تفعل وظيفة البرنامج المساعد شيئًا ما مع العنصر الذي يطلق عليه. النظر في ما يلي:
jQuery.fn.make_me_red = function() {
return this.each(function() {
this.style.color = 'red';
});
};
$('a').make_me_red();
ما يلي هو الطريقة الصحيحة
$(document).ready(function() {
$('#btnSun').click(function(){
$(this).myFunction();
});
$.fn.myFunction = function() {
alert('hi');
}
});
جرب هذا $('div').myFunction();
هذا يجب أن يعمل
$(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');
};
});
وضع ' ؛ بعد تعريف الوظيفة ...
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");
});
وظيفة الإعلان و رد في مسج.
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();
لا تنتمي إلى StackOverflow