Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript] Sprawdzenie co wywołało zdarzenie
dariolee
post
Post #1





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 18.04.2007

Ostrzeżenie: (0%)
-----


Witam,
Jak sprawdzić jaki element wywołał zdarzenie ?

  1. <div style="background:green" onmouseout="alert('poza');"><p style="background:red">fdsafdasfdasfdasfadfdasfdsa</p>
  2. <p style="background:blue">fdsafdafdasfdsafdasfasdfdasfdas</p></div>

I teraz jak sprawdzić czy alert wywołał div czy ktoryś ze znaczników <p> w tym divie??
Go to the top of the page
+Quote Post
#luq
post
Post #2





Grupa: Zarejestrowani
Postów: 589
Pomógł: 91
Dołączył: 22.05.2008
Skąd: Gliwice

Ostrzeżenie: (0%)
-----


Obiekt Event ma właściwość target (w przeglądarkach zgodnych z modelem DOM) w IE jest to srcElement.
Go to the top of the page
+Quote Post
dariolee
post
Post #3





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 18.04.2007

Ostrzeżenie: (0%)
-----


A może jakiś przykładowy kodzik ?
Go to the top of the page
+Quote Post
erix
post
Post #4





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




A może jakieś przykładowe Twoje szukanie z Google?
Go to the top of the page
+Quote Post
dariolee
post
Post #5





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 18.04.2007

Ostrzeżenie: (0%)
-----


Znalazłem coś takiego:
Kod
function functionName(event) {
    var e = event || window.event;

   if( e.target ){
     var node = e.target;
     alert(node+ 'node'); }
   else{
     var node = e.srcElement;
     alert(node+ 'node');
    }
}

I na Firefoxie alertem pokazuje mi np: [object HTMLDivElement] a w IE samo [object]. Da się jakoś z ie wyciągnąć co to było konkretnie czy div czy <p> itp ?
Go to the top of the page
+Quote Post
nospor
post
Post #6





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




node.tagName

Interesuj sie wlasciwosciami obiektow, ktorych uzywasz
Go to the top of the page
+Quote Post
dariolee
post
Post #7





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 18.04.2007

Ostrzeżenie: (0%)
-----


W końcu znalazłem ciekawe rozwiązanie:

Kod
function catchevent() {
eventSrcID=(event.srcElement)?event.srcElement.id:'undefined';
eventtype=event.type;
status=eventSrcID+' has received a '+eventtype+' event.';
}


pokazujące id elementu nad którym jest kursor myszki z tym, że jest to pod IE.
Próbowałem przerobic to na FF zamieniając srcElement na target ale nie poszło. Gdzie popełniłem błąd ? W FF w konsoli błędów pokazuje mi, że event nie jest zdefiniowany.

Kod
function catcheventFireFox() {
eventSrcID=(event.target)?event.target.id:'undefined';
eventtype=event.type;
status=eventSrcID+' has received a '+eventtype+' event.';
}

próbowałem i tak
Kod
function catcheventFireFox(event) {
var e = event || window.event;
eventSrcID=(e.target)?e.target.id:'undefined';
eventtype=e.type;
status=eventSrcID+' has received a '+eventtype+' event.';
}
Go to the top of the page
+Quote Post
zegarek84
post
Post #8





Grupa: Zarejestrowani
Postów: 1 332
Pomógł: 294
Dołączył: 12.10.2008
Skąd: Olkusz

Ostrzeżenie: (0%)
-----


http://www.quirksmode.org/js/events_mouse.html#relatedtarget
i też niżej Cross–browser scripts... ogólnie przejrzyj całą tamtą stronę a nawet cały dział o zdarzeniach event...

Event example

Ten post edytował zegarek84 22.07.2009, 10:53:01
Go to the top of the page
+Quote Post
sebax3x
post
Post #9





Grupa: Zarejestrowani
Postów: 43
Pomógł: 0
Dołączył: 8.03.2008

Ostrzeżenie: (0%)
-----


Też mam podobny problem - mimo usilnych poszukiwań nie znalazłem rozwiązania.
Stosuje funkcję:

  1. $(this).click(function () {
  2. var e = event || window.event;
  3. if( e.target ){
  4. var node = e.target;
  5. alert(node.tagName+ 'node'); }
  6. else{
  7. var node = e.srcElement;
  8. alert(node.tagName+ 'node');
  9. }
  10. }


na elemencie (img), pod Chrome pokazuje mi po kliknięciu na ten element (obrazek) to co chcę uzyskać czyli, "IMG", typ elementu, natomiast pod Firefoksem alert nie jest wyświetlany - jak ta funkcja powinna wyglądać dla Firefoxa?
Go to the top of the page
+Quote Post
nospor
post
Post #10





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




nie: function () {
a chyba raczej: function (event) {
ale glowy nie daje bo dawno nie bawilem sie eventami
Go to the top of the page
+Quote Post
sebax3x
post
Post #11





Grupa: Zarejestrowani
Postów: 43
Pomógł: 0
Dołączył: 8.03.2008

Ostrzeżenie: (0%)
-----


Właśnie o to chodziło, dzięki wielkie (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 01:14