原生input添加删除图标类似vue里面移入显示删除[jquery]

css 复制代码
 <input type="text" id="servicer-search" class="form-control" autocomplete="off" />

上面是刚开始的input

css 复制代码
<div class="servicer-search-box">
	<input type="text" id="servicer-search" class="form-control" autocomplete="off" />
	<i class="font-v2 icon-shurukuangneishanchu icons"></i> //删除图标
</div>
使用css吧icon定位到input框后面
css 复制代码
// 初始化检查input值并设置图标显示状态
    function initDeleteIcon() {
        var servicerInput = $('input[name="servicerUserId"]');
        var deleteIcon = $('.icons');
        if (servicerInput.val() && servicerInput.val().trim() !== '') {
            // 如果有值,设置鼠标移入移出事件
            setupIconHover();
        } else {
            // 如果没有值,移除事件监听
            deleteIcon.hide();
            $('#servicer-search').off('mouseenter mouseleave');
        }
    }
    
    // 设置图标悬停事件
    function setupIconHover() {
        var deleteIcon = $('.icons');
        var searchInput = $('#servicer-search');
        searchInput.on('mouseenter', function() {
            if ($('input[name="servicerUserId"]').val() && 			$('input[name="servicerUserId"]').val().trim() !== '') {
                deleteIcon.show();
            }
        }).on('mouseleave', function() {
            deleteIcon.hide();
        });
        
        // 点击删除图标清空值
        deleteIcon.on('click', function() {
       		//清空input
        });
    }
function updateDeleteIconVisibility() {
    var servicerInput = $('input[name="servicerUserId"]');
    var deleteIcon = $('.icons');
    
    if (servicerInput.val() && servicerInput.val().trim() !== '') {
        setupIconHover(); // 确保事件已绑定
    } else {
        deleteIcon.hide();
    }
}
initDeleteIcon();
updateDeleteIconVisibility()

已上是我最初时候的想法 但是做完之后发现使用mouseenter mouseleave 移上去的时候就会一直闪 所以又做了第二次改进

css 复制代码
// 初始化检查input值并设置图标显示状态
function initDeleteIcon() {
    var servicerInput = $('input[name="servicerUserId"]');
    var deleteIcon = $('.icons');

    if (servicerInput.val() && servicerInput.val()!='0/0' && servicerInput.val().trim() !== '') {
        // 如果有值,设置鼠标移入移出事件
        setupIconHover();
    } else {
        // 如果没有值,移除事件监听
        deleteIcon.hide();
        $('#servicer-search').off('mouseenter mouseleave');
        deleteIcon.off('mouseenter mouseleave');
    }
}

// 设置图标悬停事件
function setupIconHover() {
    var deleteIcon = $('.icons');
    var searchInput = $('#servicer-search');
    var hoverTimer = null;

    // 使用mouseenter和mouseleave处理整个区域的悬停
    searchInput.off('mouseenter mouseleave').on('mouseenter', function() {
        if ($('input[name="servicerUserId"]').val() && $('input[name="servicerUserId"]').val().trim() !== '' && $('input[name="servicerUserId"]').val()!='0/0') {
            // 清除之前的隐藏定时器
            if (hoverTimer) {
                clearTimeout(hoverTimer);
                hoverTimer = null;
            }
            deleteIcon.show();
        }
    }).on('mouseleave', function(e) {
        // 设置延迟隐藏,避免闪烁
        hoverTimer = setTimeout(function() {
            deleteIcon.hide();
        }, 100);
    });

    // 为删除图标添加事件处理,防止在图标上时隐藏
    deleteIcon.off('mouseenter mouseleave').on('mouseenter', function() {
        // 鼠标移到图标上时,清除隐藏定时器
        if (hoverTimer) {
            clearTimeout(hoverTimer);
            hoverTimer = null;
        }
    }).on('mouseleave', function() {
        // 鼠标离开图标时隐藏
        deleteIcon.hide();
    });

    // 点击删除图标清空值
    deleteIcon.off('click').on('click', function() {
	    //清空值
}

function updateDeleteIconVisibility() {
    var servicerInput = $('input[name="servicerUserId"]');
    var deleteIcon = $('.icons');

    if (servicerInput.val() && servicerInput.val().trim() !== '') {
        setupIconHover(); // 确保事件已绑定
    } else {
        deleteIcon.hide();
    }
}
相关推荐
余防26 分钟前
XXE - 实体注入(xml外部实体注入)
xml·前端·安全·web安全·html
jump_jump26 分钟前
前端部署工具 PinMe
运维·前端·开源
Baklib梅梅1 小时前
优秀文档案例解析:打造高效用户体验的最佳实践
前端·ruby on rails·前端框架·ruby
慧一居士1 小时前
VUE、jquery、React、Ant Design、element ui、bootstrap 前端框架的 功能总结,示例演示、使用场景介绍、完整对比总结
前端
GISer_Jing1 小时前
0926第一个口头OC——快手主站前端
开发语言·前端·javascript
MediaTea2 小时前
Jupyter Notebook:基于 Web 的交互式编程环境
前端·ide·人工智能·python·jupyter
少年阿闯~~2 小时前
CSS——重排和重绘
前端
奶糖 肥晨3 小时前
Uniapp 开发中遭遇「可选链赋值」语法陷阱:一次编译错误排查实录
javascript·vue.js·uni-app
个人看法3 小时前
h5实现一个吸附在键盘上的工具栏
前端·javascript·vue
belldeep3 小时前
python:Django 和 Vue.js 技术栈解析
vue.js·python·django