views:

381

answers:

2

<option> doesn't respond click/contextmenu events in IE8?

Here is all the code to verify it locally:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
 <meta http-equiv="Content-Language" content="en-us" />
 <title>International Properties</title>
 <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js"&gt;&lt;/script&gt;
</head>
<body>
<script type="text/javascript">
$(function(){
 $('option').bind('click contextmenu',function(){
  alert(1);
 });
});
</script>
<select size="2">
<option class="showme" id="article1">test1</option>
<option class="showme" id="article2">test2</option>
</select>
</body>
</html>

EDIT

The code I provided is to clarify the issue I met.

Eventually I'll do something like:

$('option').contextMenu('myMenu1'...
A: 

I think you need to bind the events to select instead of option:

<!DOCTYPE html>
<html>
  <head>
   <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
   <meta http-equiv="Content-Language" content="en-us" />
   <title>International Properties</title>
   <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js"&gt;&lt;/script&gt;
  </head>
  <body>
    <script type="text/javascript">
      $(function(){
       $("select").bind("click contextmenu", function(){
        alert(1);
       });
      });
    </script>
    <select size="2">
      <option value="article1">test1</option>
      <option value="article2">test2</option>
    </select>
  </body>
</html>
svinto
No,I need to bind to the `option`.Because eventually I need to pop up a context menu when mouse is on the `option`.
as in: mouseover? or what?
just somebody
Not possible using selects and options. You need to make your own dropdown to make this work.
svinto
+1  A: 

bind the handler to the select, use Event.target to get at the option the user clicked. this works for me:

$(function(){
 $('select').bind('click contextmenu',function(ev){
  console.log($(ev.target).val());
  return false;
 });
});

edit

i've tested it in ie6, ie7 and ie8 on http://ipinfo.info/netrenderer/index.php, using the following code. the last option comes up selected, displays "click() on: article2".

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
 <meta http-equiv="Content-Language" content="en-us" />
 <title>option.click</title>
 <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
</head>
<body>
<script type="text/javascript">
$(function(){
 $('select').bind('click', function (ev)
 {
  ev.target.selected = true;
  $('body').append('click() on: ' + ev.target.id);
 });
 $('option:last').click();
});
</script>
<select size="2">
<option class="showme" id="article1">test1</option>
<option class="showme" id="article2">test2</option>
</select>
</body>
</html>
just somebody
I didnt set value to `option`
it's just an example. the important part is that `ev.target` is just what you're after.
just somebody
I just tried,not working,it can't detect which option is currently being clicked.