这是IE8还是jQuery的错误?

问题描述 投票:0回答:2

<option>不响应IE8中的click / contextmenu事件?

这里是在本地进行验证的所有代码:

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

<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"></script>
</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>

编辑

我提供的代码是为了澄清我遇到的问题。

最终我会做类似的事情:

$('option').contextMenu('myMenu1'...
jquery internet-explorer-8 contextmenu option jquery-events
2个回答
1
投票

将处理程序绑定到选择,使用Event.target获得用户单击的选项。这对我有用:

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

edit

我已经使用以下代码在http://ipinfo.info/netrenderer/index.php的ie6,ie7和ie8中对其进行了测试。最后一个选项被选中,显示“ click()on:article2”。

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

<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"></script>
</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>

0
投票

我认为您需要将事件绑定到select而不是选项:

<!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"></script>
  </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>
© www.soinside.com 2019 - 2024. All rights reserved.