原生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();
    }
}
相关推荐
文心快码BaiduComate8 分钟前
双十一将至,用Rules玩转电商场景提效
前端·人工智能·后端
拉不动的猪11 分钟前
关于scoped样式隔离原理和失效情况&&常见样式隔离方案
前端·javascript·面试
摇滚侠11 分钟前
Vue 项目实战《尚医通》,医院详情菜单与子路由,笔记17
前端·vue.js·笔记
有来技术14 分钟前
vite-plugin-vue-mcp:在 Vue 3 + Vite 中启用 MCP,让 AI 理解并调试你的应用
前端·vue.js·人工智能
fruge18 分钟前
前端本地存储进阶:IndexedDB 封装与离线应用开发
前端
忍者扔飞镖24 分钟前
欧服加载太慢了,咋整
前端·性能优化
鹏北海34 分钟前
Vue 3 超强二维码识别:多区域/多尺度扫描 + 高级图像处理
前端·javascript·vue.js
Android疑难杂症36 分钟前
一文讲清鸿蒙网络开发
前端·javascript·harmonyos
爱学习的程序媛38 分钟前
【JavaScript基础】Null类型详解
前端·javascript
前端一课42 分钟前
uniapp之WebView容器原理详解
前端