这是 IE8 还是 jQuery 的错误吗?

发布于 2024-08-13 18:51:50 字数 1138 浏览 1 评论 0原文

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

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

<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>

EDIT

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

Eventually I'll do something like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

比忠 2024-08-20 18:51:50

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

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

编辑

我已经在 ie6、ie7 和 ie8 上测试了它 http://ipinfo.info/netrenderer/index.php,使用以下代码。最后一个选项被选中,显示“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>

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

<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>
生生漫 2024-08-20 18:51:50

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

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"></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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文