admin管理员组

文章数量:1025810

I'm trying to get the target/srcElement of an HTML "area" element for an image map.

Here's what I have so far (pardon the data URI):

<map name="dpad">
  <area id="dpad"shape="rect" alt="" title="" coords="1,14,12,24" onmousedown="move('l',false,event);" target="" />
  <area shape="rect" alt="" title="" coords="15,2,24,12" onmousedown="move('u');" target="" />
  <area shape="rect" alt="" title="" coords="27,15,38,24" onmousedown="move('r');" target="" />
  <area shape="rect" alt="" title="" coords="15,27,25,38" onmousedown="move('d');" target=""/>
</map>
<img width="40"height="40"src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTAw9HKhAAADDElEQVRYR+2ZvYoiQRSFW1EMVkEzI2XfydQH2GwGDTfczFRYNNBBUDFQUFN9ATXQwEEDRQ3UB3ADQajpr7Gkp8f+k2G6F2bg0jVVt+qcrp9zb5cBxeVfMpmMnE6nX+fz+U8kEvkRCoUsR7hcLorq+0/1/R2NRv8ej8ezS0h37uFwOBEMBkupVErk83lRKBQsDR986UNfd2gPeAcCgYRqpUwmI+bzuTgcDjcbj8cC09fhgy996PsApLsukmA2mxWLxeJGhnKz2dTMWI+vpwRXq5XodDqi3W5rRpk6ZhKynhLc7XZiMBiIVqsler2eZpSpo81zgqPRSDQaDdHtdkW/39eMMnXsR08JTqdTUSqVRK1WE/V6/Z1RVy6XBT6eLTEzNJlMLA0fzwjqT6teWvRlT5f4myASnU6nI4SgqwA7ef5UfV+MQu1giV/UfvR1gpGAE9wUNYA/EyevSu/kCdDrAwRfeTGnOHCKxWLPipqNnAjmxEtAnVqxWBTr9fpd3L03i/jg63Rc/OACJ7gxg1pWQlBn0+sNDZNia2wDeL/f3wgSNWazmWaUJVl88DX253/GBsPYBhc4wU2Jx+NauqR/ewCIEIgwOme2v6jfbDZiOByKarWqCTZWqVS0UEebVV/GBgMs/UvRB05w+0CQIM/ghCrA7AhCjihCDJahjjJ1tNkRBAMsMGWCYUqQqSYTIdgTTwGxIshbM3N6cnqStG23W1OSjA0GWGCCLbX1wwyyH8jlSJckoB1B9hszIEkZn7ThYzaLkiD9wAQbDnD5/wjylsvl8lOXmMPidonh8GWHhI3/qYdEDsbmZx+Qz9md4nsyw+HgBDuRGTDAMpUZxDCXy5kKNQTviaxRqFlKKdT6ZbUSasY2E2o4aULt+1BHQPZ1suD7dMvdZ7uimH24O8gHvb1Z+CbodKn1Sywvi+yenn12omEIrUyZSJuk+eLDXWbFvr36gKDvL484vb6+fpPy4rsLTN9eAesv0ck47C7R8fnSS3R+hlDToCeV6Il0iE9DK8Pn6vtEX6d6K/3eAH0oKaUOiMCyAAAAAElFTkSuQmCC"usemap="#dpad"/>

So, for the left arrow on the dpad, I have a function to be called onmousedown, move(dir,hold,e).

Here is the function started:

function move(dir,hold,e){
    var p=document.getElementById('test');
    alert(e);
}

This is confusing the crap out of me as to why I'm getting "undefined" when clicking on the area. Could someone please explain in FULL detail as to how I can get the target?

Do I need to include 'event' as a parameter in my HTML code? Do I even NEED a parameter for it?

On another note, here's something that really confused me.

So, I can't get the target of that element, but this works just fine:

<div class="male1-d" id="test" data-sprite="male1" onclick="alert(event.target+' '+this.style.left+' '+this.style.top);"></div>

Done. Target obtained. Now why can't I do it in a function?

I forgot to mention that for Opera I meant mobile(?) version 9.50.

I'm trying to get the target/srcElement of an HTML "area" element for an image map.

Here's what I have so far (pardon the data URI):

<map name="dpad">
  <area id="dpad"shape="rect" alt="" title="" coords="1,14,12,24" onmousedown="move('l',false,event);" target="" />
  <area shape="rect" alt="" title="" coords="15,2,24,12" onmousedown="move('u');" target="" />
  <area shape="rect" alt="" title="" coords="27,15,38,24" onmousedown="move('r');" target="" />
  <area shape="rect" alt="" title="" coords="15,27,25,38" onmousedown="move('d');" target=""/>
</map>
<img width="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dT6ZbUSasY2E2o4aULt+1BHQPZ1suD7dMvdZ7uimH24O8gHvb1Z+CbodKn1Sywvi+yenn12omEIrUyZSJuk+eLDXWbFvr36gKDvL484vb6+fpPy4rsLTN9eAesv0ck47C7R8fnSS3R+hlDToCeV6Il0iE9DK8Pn6vtEX6d6K/3eAH0oKaUOiMCyAAAAAElFTkSuQmCC"usemap="#dpad"/>

So, for the left arrow on the dpad, I have a function to be called onmousedown, move(dir,hold,e).

Here is the function started:

function move(dir,hold,e){
    var p=document.getElementById('test');
    alert(e);
}

This is confusing the crap out of me as to why I'm getting "undefined" when clicking on the area. Could someone please explain in FULL detail as to how I can get the target?

Do I need to include 'event' as a parameter in my HTML code? Do I even NEED a parameter for it?

On another note, here's something that really confused me.

So, I can't get the target of that element, but this works just fine:

<div class="male1-d" id="test" data-sprite="male1" onclick="alert(event.target+' '+this.style.left+' '+this.style.top);"></div>

Done. Target obtained. Now why can't I do it in a function?

I forgot to mention that for Opera I meant mobile(?) version 9.50.

Share Improve this question edited Jan 14, 2013 at 6:01 Derek 朕會功夫 94.5k45 gold badges198 silver badges253 bronze badges asked Jan 14, 2013 at 4:10 Ricky YoderRicky Yoder 5713 gold badges11 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

in some browsers,'event' is a attribute of window.But you need to include 'event' as a parameter in others.So your move function should be like below:

 <html>
   <body>
     <button onmousedown='move(event)' id='btn' name='a button'>click</button>
   </body>
   <script>
     function move(e){
       var e=e||window.event;
       var target=e.target||e.srcElement;
       alert(target);
       alert(target.id);
       alert(target.name);
    }
  </script>
 </html>

It seems , it is a problem in the click event. They says , it is a bug in jquery. And has given details in

Jquery click event of a div in li element not fired in mobile Safari

It says two things:

Add cursor:pointer to the DIV class.
Add onclick="" to the element 

I'm trying to get the target/srcElement of an HTML "area" element for an image map.

Here's what I have so far (pardon the data URI):

<map name="dpad">
  <area id="dpad"shape="rect" alt="" title="" coords="1,14,12,24" onmousedown="move('l',false,event);" target="" />
  <area shape="rect" alt="" title="" coords="15,2,24,12" onmousedown="move('u');" target="" />
  <area shape="rect" alt="" title="" coords="27,15,38,24" onmousedown="move('r');" target="" />
  <area shape="rect" alt="" title="" coords="15,27,25,38" onmousedown="move('d');" target=""/>
</map>
<img width="40"height="40"src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTAw9HKhAAADDElEQVRYR+2ZvYoiQRSFW1EMVkEzI2XfydQH2GwGDTfczFRYNNBBUDFQUFN9ATXQwEEDRQ3UB3ADQajpr7Gkp8f+k2G6F2bg0jVVt+qcrp9zb5cBxeVfMpmMnE6nX+fz+U8kEvkRCoUsR7hcLorq+0/1/R2NRv8ej8ezS0h37uFwOBEMBkupVErk83lRKBQsDR986UNfd2gPeAcCgYRqpUwmI+bzuTgcDjcbj8cC09fhgy996PsApLsukmA2mxWLxeJGhnKz2dTMWI+vpwRXq5XodDqi3W5rRpk6ZhKynhLc7XZiMBiIVqsler2eZpSpo81zgqPRSDQaDdHtdkW/39eMMnXsR08JTqdTUSqVRK1WE/V6/Z1RVy6XBT6eLTEzNJlMLA0fzwjqT6teWvRlT5f4myASnU6nI4SgqwA7ef5UfV+MQu1giV/UfvR1gpGAE9wUNYA/EyevSu/kCdDrAwRfeTGnOHCKxWLPipqNnAjmxEtAnVqxWBTr9fpd3L03i/jg63Rc/OACJ7gxg1pWQlBn0+sNDZNia2wDeL/f3wgSNWazmWaUJVl88DX253/GBsPYBhc4wU2Jx+NauqR/ewCIEIgwOme2v6jfbDZiOByKarWqCTZWqVS0UEebVV/GBgMs/UvRB05w+0CQIM/ghCrA7AhCjihCDJahjjJ1tNkRBAMsMGWCYUqQqSYTIdgTTwGxIshbM3N6cnqStG23W1OSjA0GWGCCLbX1wwyyH8jlSJckoB1B9hszIEkZn7ThYzaLkiD9wAQbDnD5/wjylsvl8lOXmMPidonh8GWHhI3/qYdEDsbmZx+Qz9md4nsyw+HgBDuRGTDAMpUZxDCXy5kKNQTviaxRqFlKKdT6ZbUSasY2E2o4aULt+1BHQPZ1suD7dMvdZ7uimH24O8gHvb1Z+CbodKn1Sywvi+yenn12omEIrUyZSJuk+eLDXWbFvr36gKDvL484vb6+fpPy4rsLTN9eAesv0ck47C7R8fnSS3R+hlDToCeV6Il0iE9DK8Pn6vtEX6d6K/3eAH0oKaUOiMCyAAAAAElFTkSuQmCC"usemap="#dpad"/>

So, for the left arrow on the dpad, I have a function to be called onmousedown, move(dir,hold,e).

Here is the function started:

function move(dir,hold,e){
    var p=document.getElementById('test');
    alert(e);
}

This is confusing the crap out of me as to why I'm getting "undefined" when clicking on the area. Could someone please explain in FULL detail as to how I can get the target?

Do I need to include 'event' as a parameter in my HTML code? Do I even NEED a parameter for it?

On another note, here's something that really confused me.

So, I can't get the target of that element, but this works just fine:

<div class="male1-d" id="test" data-sprite="male1" onclick="alert(event.target+' '+this.style.left+' '+this.style.top);"></div>

Done. Target obtained. Now why can't I do it in a function?

I forgot to mention that for Opera I meant mobile(?) version 9.50.

I'm trying to get the target/srcElement of an HTML "area" element for an image map.

Here's what I have so far (pardon the data URI):

<map name="dpad">
  <area id="dpad"shape="rect" alt="" title="" coords="1,14,12,24" onmousedown="move('l',false,event);" target="" />
  <area shape="rect" alt="" title="" coords="15,2,24,12" onmousedown="move('u');" target="" />
  <area shape="rect" alt="" title="" coords="27,15,38,24" onmousedown="move('r');" target="" />
  <area shape="rect" alt="" title="" coords="15,27,25,38" onmousedown="move('d');" target=""/>
</map>
<img width="40"height="40"src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAAD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DToCeV6Il0iE9DK8Pn6vtEX6d6K/3eAH0oKaUOiMCyAAAAAElFTkSuQmCC"usemap="#dpad"/>

So, for the left arrow on the dpad, I have a function to be called onmousedown, move(dir,hold,e).

Here is the function started:

function move(dir,hold,e){
    var p=document.getElementById('test');
    alert(e);
}

This is confusing the crap out of me as to why I'm getting "undefined" when clicking on the area. Could someone please explain in FULL detail as to how I can get the target?

Do I need to include 'event' as a parameter in my HTML code? Do I even NEED a parameter for it?

On another note, here's something that really confused me.

So, I can't get the target of that element, but this works just fine:

<div class="male1-d" id="test" data-sprite="male1" onclick="alert(event.target+' '+this.style.left+' '+this.style.top);"></div>

Done. Target obtained. Now why can't I do it in a function?

I forgot to mention that for Opera I meant mobile(?) version 9.50.

Share Improve this question edited Jan 14, 2013 at 6:01 Derek 朕會功夫 94.5k45 gold badges198 silver badges253 bronze badges asked Jan 14, 2013 at 4:10 Ricky YoderRicky Yoder 5713 gold badges11 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

in some browsers,'event' is a attribute of window.But you need to include 'event' as a parameter in others.So your move function should be like below:

 <html>
   <body>
     <button onmousedown='move(event)' id='btn' name='a button'>click</button>
   </body>
   <script>
     function move(e){
       var e=e||window.event;
       var target=e.target||e.srcElement;
       alert(target);
       alert(target.id);
       alert(target.name);
    }
  </script>
 </html>

It seems , it is a problem in the click event. They says , it is a bug in jquery. And has given details in

Jquery click event of a div in li element not fired in mobile Safari

It says two things:

Add cursor:pointer to the DIV class.
Add onclick="" to the element 

本文标签: javascripteventtargetsrcElement not working 100 in Chrome and OperaStack Overflow