Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
thêm hàm cho onclick
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dau Hai  
View profile   Translate to Translated (View Original)
 More options Jan 6 2010, 12:10 pm
From: Dau Hai <dauthanh...@gmail.com>
Date: Thu, 7 Jan 2010 00:10:17 +0700
Local: Wed, Jan 6 2010 12:10 pm
Subject: thêm hàm cho onclick

Em có một input có thuộc tính onclick="doSomeFunction();" bây giờ muốn thêm
một hàm nữa ví dụ như onclick="doSomeFunction(); doSomeFunction2();". Công
việc này có làm bằng Javascript được không ah? Em cám ơn mọi người.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Huynh Quoc Huy  
View profile   Translate to Translated (View Original)
 More options Jan 6 2010, 8:41 pm
From: Huynh Quoc Huy <huynhquoc...@gmail.com>
Date: Thu, 7 Jan 2010 08:41:27 +0700
Local: Wed, Jan 6 2010 8:41 pm
Subject: Re: [PHPVietnam] thêm hàm cho onclick

Tốt nhất bạn nên gọi hàm doSomeFunction2() trong doSomeFunction();


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hưng Nguyễn  
View profile   Translate to Translated (View Original)
 More options Jan 7 2010, 3:22 am
From: Hưng Nguyễn <hun...@gmail.com>
Date: Thu, 7 Jan 2010 00:22:23 -0800 (PST)
Local: Thurs, Jan 7 2010 3:22 am
Subject: Re: thêm hàm cho onclick
On Jan 7, 8:41 am, Huynh Quoc Huy <huynhquoc...@gmail.com> wrote:

> Tốt nhất bạn nên gọi hàm doSomeFunction2() trong doSomeFunction();

Đây là cách dễ nhất, k phải tốt nhất.

Pure JS code:

input = document.getElementById('input id cua ban');
input.onclick += do_some_things;
input.onclick += do_more_things

jQuery hoặc các JS framework có sẵn sẽ giúp bạn làm việc này dễ hơn.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hoatle  
View profile   Translate to Translated (View Original)
 More options Jan 7 2010, 3:31 am
From: hoatle <hoatle...@gmail.com>
Date: Thu, 7 Jan 2010 00:31:13 -0800 (PST)
Local: Thurs, Jan 7 2010 3:31 am
Subject: Re: thêm hàm cho onclick
Thứ nhất về event handler cho các element, bạn nên tránh dùng inline
như:

<input type="text" onclick="doSomething()" />

Mà nên đặt cho nó 1 cái id rồi gắn event handler vào:

<input type="text" id="idRef" />

Sau đó trong file js th́ làm thế này:

var inputEl = document.getElementById('idRef');
inputEl.onclick = function() {
  doSomeThing();
  doSomeThing2();

}

Chú ư muốn làm thế này th́ bạn phải đảm bảo dom được load xong (xem ở
đây: http://www.javascriptkit.com/dhtmltutors/domready.shtml).

Nếu như dùng trong jQuery:

$(document).ready(function() {
//code của bạn đặt ở đây sẽ được chạy khi dom load xong.

});

Nếu ko phân biệt thứ tự nhiều event handler có thể dùng
addEventListener như sau (code tớ vẫn đang dùng và ko có vấn đề ǵ).
Cái removeEventListener chưa dùng lần nào :P.

/**
 * Cross browser add event listener method. For 'evt' pass a string
value with the leading "on" omitted
 * e.g. Util.addEventListener
(window,'load',myFunctionNameWithoutParenthesis,false);
 * @param       obj object to attach event
 * @param       evt event name: click, mouseover, focus, blur...
 * @param       func    function name
 * @param       useCapture      true or false; if false => use bubbling
 * @static
 * @author      hoatle
 * @see         http://phrogz.net/JS/AttachEvent_js.txt
 */
function addEventListener(obj, evt, fnc, useCapture) {
        if (obj === null || evt === null || fnc ===  null || useCapture ===
null) {
                debug.warn('all params is required from Util.addEventListener!');
                return;
        }
        if (!useCapture) useCapture = false;
        if (obj.addEventListener){oatle
                obj.addEventListener(evt, fnc, useCapture);
        } else if (obj.attachEvent) {
                obj.attachEvent('on'+evt, fnc);
        } else{
                myAttachEvent(obj, evt, fnc);
                obj['on'+evt] = function() { myFireEvent(obj,evt) };
        }

        //The following are for browsers like NS4 or IE5Mac which don't
support either
        //attachEvent or addEventListener
        var myAttachEvent = function(obj, evt, fnc) {
                if (!obj.myEvents) obj.myEvents={};
                if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
                var evts = obj.myEvents[evt];
                evts[evts.length] = fnc;
        }

        var myFireEvent = function(obj, evt) {
                if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
                var evts = obj.myEvents[evt];
                for (var i=0,len=evts.length;i<len;i++) evts[i]();
        }

}

/**
 * removes event listener.
 * @param       obj element
 * @param       evt event name, 'click', 'blur'. 'focus'...
 * @func        function name to be removed if found
 * @author      hoatle
 * @static
 * //TODO make sure method cross-browsered
 */
function removeEventListener(obj, evt, func) {
        if (obj.removeEventListener) {
                obj.removeEventListener(evt, func, false);
        } else if (obj.detachEvent) {//IE
                obj.detachEvent('on'+evt, func)
        }

}

Sau đó add các thể loại event handler vào:

function doSomething() {alert('doSomething()');}
function doSomething2() {alert('doSomething2()');}
function doSomethingN() {alert('doSomethingN()');}

var inputEl = document.getElemntById('idRef');
addEventListener(inputEl, 'click', doSomething, false);
addEventListener(inputEl, 'click', doSomething2, false);
addEventListener(inputEl, 'click', doSomethingN, false);

Nên dùng cái này nếu muốn có nhiều event handler (nhưng ko phân biệt
thứ tự cái này chạy trước, chạy sau) cho một element.
Lưu ư: hiện tại chỉ biết addEventListener vào thôi chứ chưa biết là có
bao nhiêu event handler, sau này sẽ có :D.
Cheer! :D

On Jan 7, 8:41 am, Huynh Quoc Huy <huynhquoc...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Huynh Quoc Huy  
View profile   Translate to Translated (View Original)
 More options Jan 7 2010, 3:37 am
From: Huynh Quoc Huy <huynhquoc...@gmail.com>
Date: Thu, 7 Jan 2010 15:37:26 +0700
Local: Thurs, Jan 7 2010 3:37 am
Subject: Re: [PHPVietnam] Re: thêm hàm cho onclick

Cám ơn bạn nhiều !


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hoatle  
View profile   Translate to Translated (View Original)
 More options Jan 7 2010, 3:47 am
From: hoatle <hoatle...@gmail.com>
Date: Thu, 7 Jan 2010 00:47:48 -0800 (PST)
Local: Thurs, Jan 7 2010 3:47 am
Subject: Re: thêm hàm cho onclick
Ặk, quên mất, phải bỏ ḍng này đi trong addEventListener:

debug.warn('all params is required from Util.addEventListener!');

Trong quá tŕnh code, tớ sợ nhất dùng alert để debug.
Để logging dùng debug này là hay nhất: http://benalman.com/projects/javascript-debug-console-log/

On Jan 7, 3:37 pm, Huynh Quoc Huy <huynhquoc...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Huynh Quoc Huy  
View profile   Translate to Translated (View Original)
 More options Jan 7 2010, 3:52 am
From: Huynh Quoc Huy <huynhquoc...@gmail.com>
Date: Thu, 7 Jan 2010 15:52:56 +0700
Local: Thurs, Jan 7 2010 3:52 am
Subject: Re: [PHPVietnam] Re: thêm hàm cho onclick

Ḿnh thấy thư viện bạn dùng hay được sử dụng trong Flash lắm. Trước đây
không hiểu lắm nhưng giờ th́ hiểu rơ hơn :D
Cám ơn bạn một lần nữa. Và đặc biệt cái này
http://benalman.com/projects/javascript-debug-console-log/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dau Thanh Hai  
View profile   Translate to Translated (View Original)
 More options Jan 7 2010, 4:57 am
From: Dau Thanh Hai <dauthanh...@gmail.com>
Date: Thu, 07 Jan 2010 16:57:23 +0700
Local: Thurs, Jan 7 2010 4:57 am
Subject: Re: [PHPVietnam] Re: thêm hàm cho onclick
On 1/7/2010 3:31 PM, hoatle wrote:

Nếu sử dụng EventListener th́ truyền tham số vào thế nào được ạ?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hoatle  
View profile   Translate to Translated (View Original)
 More options Jan 7 2010, 5:10 am
From: hoatle <hoatle...@gmail.com>
Date: Thu, 7 Jan 2010 02:10:06 -0800 (PST)
Local: Thurs, Jan 7 2010 5:10 am
Subject: Re: thêm hàm cho onclick
Có một số cách để truyền tham số như sau:

function doSomething(param1, param2) {
  alert(this);

}

addEventListener(el, 'click', function() {
  doSomething(1, 2);

}, false);

Khi sử dụng cách inline th́ this trong thằng event handler nó refer
luôn đến element đó nên rất tiện khi lấy value; có thể dùng từ khóa
this refer đến element theo cách addEventListener theo cách này:

addEventListener(el, 'click', function() {
  doSomething(1, 2); // alert ra thằng window element
  //Để alert ra thằng el để lấy value chẳng hạn th́ dùng 1 trong 2
cách sau để truyền context:
  doSomething.call(el, 1, 2);
  //Hoặc
  doSomething.apply(el, [1, 2]);

}, false);

Xem thêm: http://blog.metawrap.com/blog/TheVeryUsefulJavaScriptCallAndApplyFunc...

à, đoạn code trên của tớ bị thừa "oatle" do khi type chạm vào cái
thằng touch pad:

if (obj.addEventListener){oatle
                obj.addEventListener(evt, fnc, useCapture);
        } else if (obj.attachEvent) {
                obj.attachEvent('on'+evt, fnc);
        } else{
                myAttachEvent(obj, evt, fnc);
                obj['on'+evt] = function() { myFireEvent(obj,evt) };
        }

bỏ "oatle" đi nha.

On Jan 7, 4:57 pm, Dau Thanh Hai <dauthanh...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hoatle  
View profile   Translate to Translated (View Original)
 More options Jan 7 2010, 2:20 pm
From: hoatle <hoatle...@gmail.com>
Date: Thu, 7 Jan 2010 11:20:34 -0800 (PST)
Local: Thurs, Jan 7 2010 2:20 pm
Subject: Re: thêm hàm cho onclick
Tớ có viết bài này cho rơ hơn ở đây:
http://hoatle.net/vi/Gan-Nhieu-Ham-Xu-Ly-Su-Kien-Vao-1-Phan-Tu-Trong-...

On Jan 7, 5:10 pm, hoatle <hoatle...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dau Hai  
View profile   Translate to Translated (View Original)
 More options Jan 8 2010, 3:01 am
From: Dau Hai <dauthanh...@gmail.com>
Date: Fri, 8 Jan 2010 15:01:10 +0700
Local: Fri, Jan 8 2010 3:01 am
Subject: Re: [PHPVietnam] Re: thêm hàm cho onclick

Cám ơn anh HoaLe

2010/1/8 hoatle <hoatle...@gmail.com>

--
Best Regards
Đậu Thanh Hải
K51CC-Coltech-VNU

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »