jQuery判断元素是否是鼠标划过(hover)状态

17-03-15 17:44 字数 887 阅读 9685 已编辑

现在有个需求是鼠标划过父div,显示子元素ul,点击ul的子元素li的a标签,选中radio。其实就是自己实现一个下拉选择框。

html代码如下

<div class="yyjh_sel status_new_sel">  
    <label><input type="radio" name="yyjh" value="1">有意向/较好</label>  
    <ul class="child_sel">  
        <li name="g_status" id="g_yyjh_status">  
            <option value="1">有意向</option>  
        </li>  
    </ul>  
</div>  

要实现的效果如下

逻辑分析

鼠标放到radio上,显示ul,因为child_sel是绝对定位,所以在鼠标离开radio时,要判断下ul是否是hover状态,因为是使用jQuery,所以使用is(':hover')判断元素是否是hover状态。

代码如下

$(document).on('mouseover mouseout','.status_new_sel',function (e) {  
        if(e.type == 'mouseover'){  
            $(this).children('.child_sel').show();  
            var _child_a = $(".child_sel>li>a");  
            //鼠标划过变色  
            _child_a.hover(function () {  
                $(this).parent('li').css('background-color', '#eee');  
            },function () {  
                $(this).parent('li').css('background-color', '#fff');  
            });  
            //点击选中父radio  
            _child_a.click(function () {  
                $(this).parent().parent().siblings('label').children('input[type="radio"]').attr('checked','checked');  
                $(this).parent().parent('.child_sel').hide();  
            });  
        }else if (e.type == 'mouseout'){  
            var _this = $(this);  
            setTimeout(function () {  
                if (!_this.children('.child_sel').is(':hover')){  
                    _this.children('.child_sel').hide();  
                }  
            },500);  
        }  
    });  

初始效果有点丑,可以自己写css美化下。

0人点赞>
关注 收藏 改进 举报
2 条评论
排序方式 时间 投票
aijquery

www.aijquery.cn 求交换

Up骚年
https://www.shiqidu.com/friend-link.html, 这里申请下, 朋友。
请登录后发表评论
站长 @ 十七度
文章
380
粉丝
23
喜欢
190
收藏
31
排名 : 1
访问 : 127.84万
私信