【问题标题】:Is this a IE8 or jQuery's bug?这是 IE8 还是 jQuery 的错误?
【发布时间】:2025-11-24 19:50:02
【问题描述】:

<option> 不响应 IE8 中的单击/上下文菜单事件?

这是本地验证的所有代码:

<!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'...

【问题讨论】:

  • 你的问题是什么?
  • 我的问题是:如何让选项响应 IE8 中的点击?
  • 你想达到什么目的?您确定不想将点击处理程序绑定到选择吗?那会更有意义。
  • 我确定,因为最终我需要在鼠标在选项上时弹出不同的上下文菜单。说,这是一个人为的例子。

标签: jquery internet-explorer-8 contextmenu option jquery-events


【解决方案1】:

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

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

编辑

我已经在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>

【讨论】:

  • 我没有将值设置为option
  • 这只是一个例子。重要的是 ev.target 正是您所追求的。
  • 我刚试了,不行,检测不到当前点击的是哪个选项。
【解决方案2】:

我认为您需要将事件绑定到 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>

【讨论】:

  • 不,我需要绑定到option。因为最终我需要在鼠标在option上时弹出一个上下文菜单。
  • 无法使用选择和选项。您需要制作自己的下拉菜单才能完成这项工作。
最近更新 更多