0

我有以下html

<div id="active-el">
    <select id="select-drop">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <div>test test test</div>
</div>

用所说的jQuery

var active = true;

jQuery('#active-el').live('mouseenter', function () {
    active = true;
    console.log('over active = true');
});

jQuery("html").click(function () {
    if (!active) {
     alert('not selected');   
    }
});

你可以在这里看到它工作http://jsfiddle.net/hLDxM/2/。我有一个问题,当我change选择下拉菜单时 - 它变得不活跃?

我如何确保即使用户将 更改select为任何值 -(html).click事件也不会触发?也就是说 - 无论选项 1、2、3 还是 - 它仍然在其中#active-el,因此应该处于活动状态?

4

2 回答 2

0

我不确定您要做什么,但请尝试更改

    var active = true;

jQuery('#active-el').live('mouseover', function () {
    active = true;
    console.log('over active = true');
}).live('mouseout', function () {
    active = false;
    console.log('over not active = false');
});

jQuery("html").click(function () {
    if (!active) {
     alert('not selected');   
    }
});

使用 mouseout 和 mouseover 而不是 mouseenter 和 mouseleave。

你能描述一下你想完成什么吗?

于 2011-07-12T13:00:00.993 回答
0

更新:.change()最终用于解决此问题。

注意到 -.live(change,function() {});在 jQuery 1.6.2 中不起作用

于 2011-07-12T15:50:45.843 回答