【问题标题】:jQuery - triggering function after click on <option> tagjQuery - 单击 <option> 标签后触发功能
【发布时间】:2009-09-26 16:48:34
【问题描述】:

我有这段代码,可以在最新版本的 firefox、opera 中运行,我不确定是否在 IE8 中,但它在 Google Chrome、Safari 和 ofc IE7 和 6 中不起作用。

我的脚本有点复杂,但问题出在:

<select>
<option class='gramaz_vyber'>1</option>
<option class='gramaz_vyber'>2</option>     
</select>

还有 jQuery:

$('.gramaz_vyber').click(function() {
    $(this).hide();
});

正如我之前写的,我的代码看起来不同,但我需要在单击 &lt;option&gt; 后启动一个函数,它只在 FF/Opera 中有效......知道我该如何绕过它吗?

【问题讨论】:

    标签: jquery html function


    【解决方案1】:

    确实很有趣,在 IE7 中似乎不能在选项元素上使用 hide()(未在 IE8 上测试过)。

    但以下代码有效:

    <html>
    <head></head>
    
    <body>
        <select>
            <option>-</option>
            <option class="removable">1</option>
            <option class="removable">2</option>         
        </select>
    
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>  
        <script type="text/javascript">
            $(function() {
                $('select').change(function() {
                    $(this).find('option.removable:selected').remove();
                });
            });
        </script>
    
    </body>
    </html>
    

    不同之处在于您是从 DOM 中移除对象,而不是简单地隐藏它。

    【讨论】:

      【解决方案2】:

      在我的脑海中,我认为并非所有浏览器都支持&lt;option&gt; 元素的点击事件。

      您需要处理父 &lt;select&gt; 元素的更改事件。

      【讨论】:

      • 你能给我任何函数的名称或我应该寻找的东西吗?
      【解决方案3】:

      尝试更改event 示例here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-16
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-04
        • 2012-08-25
        相关资源
        最近更新 更多