|
 |
|
ถามเรื่องการสร้าง event ใน plugin ของ jquery ทำอย่างไรครับ |
|
 |
|
|
 |
 |
|
การสร้าง event ใน plugin ของ jquery ทำอย่างไรครับ
เช่นเราพัฒนา plugin ของเราเอง
//// Code Plugin ///
$.fn.MyPlugin = function(options){
var defaults = {
val1: null,
val2: null,
val3: null
};
var options = $.extend(defaults, options);
var obj = $(this);
$(obj).click(function(){
alert( val1+' , '+val2+','+val3 );
return false;
});
};
/// Code html ///
$('a.myplugin').({
val1:'1',
val2:'2',
val3:'3'
});
แล้วถ้าเราต้องการ สร้าง event ใน plug in ละ เราจะแทรกโค๊ดอย่างไรหละ ผมต้องการ event ไรสักอย่างอะครับ ยกตัวอย่างเป็น event
onsussess :
/// Code html ///
$('a.myplugin').({
val1:'1',
val2:'2',
val3:'3',
onsussess:function(data){
alert( data );
}
});
ผมสร้าง event ในโค๊ดนี้แล้วแต่ไม่รู้จะเขียนกำกับเหตุการของ event " onsussess " นี้อย่างไร
เช่นต้องการให้ เมื่อทำงานเสร็จหมดทุกอย่าง ใน plugin นี้แล้ว alert( ค่าที่ต้องการให้ alert " data " ที่กำหนดไว้หน้าเรียกใช้แล้วอะครับ )
หรือค่าไรก็ได้ครับ
//// Code Plugin ///
$.fn.MyPlugin = function(options){
var defaults = {
val1: null,
val2: null,
val3: null,
onsussess: function(){ },
};
var options = $.extend(defaults, options);
var obj = $(this);
$(obj).click(function(){
alert( val1+' , '+val2+','+val3 );
return false;
});
};
ถ้าเป็นไรได้ขอทุกความคิดเห็นเลยครับ ไม่ว่าได้หรือไม่ได้ หรือมีแนวทางใด ขอบคุณล่วงหน้าครับ
Tag : PHP, jQuery

|
|
 |
 |
 |
 |
Date :
2011-11-24 08:09:51 |
By :
t |
View :
901 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใส่ () หลังชื่อพารามิเตอร์ function เป็นการ call function ครับ
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>การเขียน jQuery Plugin</title>
<style type="text/css">
.gold {
font-family:verdana;
width:50px;
height:30px;
background-color:gold;
cursor:pointer;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
;(function($){
$.fn.MyAlert = function(options) {
var ps = $.extend({}, $.fn.MyAlert.defaults, options);
return this.each(function(){
$(this).click(function(e){
e.preventDefault();
ps.success(ps);
});
});
};
$.fn.MyAlert.defaults = {
var1:1,
var2:2,
var3:3,
success:function(ps){
alert(ps.var1+','+ps.var2+','+ps.var3);
}
};
})(jQuery);
$(function(){
$('#test1').MyAlert({var1:100});
$('#test2').MyAlert({
var1:200,
success:function(ps){
alert('['+ps.var1+']');
}
});
});
</script>
</head>
<body>
<b>ทดสอบ PlugIn</b>
<br /><br />
<a href="#" id="test1">test1</a>
<a href="#" id="test2">test2</a>
</body>
</html>
|
 |
 |
 |
 |
Date :
2011-11-24 09:49:47 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ชื่อไรครับ รู้สึกตอบให้ผมหลายครั้งแล้ว ขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2011-11-24 11:13:38 |
By :
t |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หนุ่มครับ นานๆ ตอบทีนะครับ 
|
 |
 |
 |
 |
Date :
2011-11-24 16:02:00 |
By :
ืnum |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|