原生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();
    }
}
相关推荐
梦鱼16 分钟前
Vue 项目图标一把梭:Iconify 自用小记(含 TS/JS 双版本组件)
前端·javascript·vue.js
best66617 分钟前
Flex 与 Grid 的 order 参数:布局界的 "插队神器"
前端
小噔小咚什么东东18 分钟前
看到了很多次WebRTC,但是你真的需要它吗?
前端·webrtc
猫七先生19 分钟前
微信小程序一键登录可行性方案
前端·微信小程序
维他AD钙19 分钟前
前端开发 8 个非常实用小技巧:高效解决日常开发痛点
前端
光影少年23 分钟前
webpack和vite优化方案都有哪些
前端·webpack·node.js
给月亮点灯|25 分钟前
Vue基础知识-脚手架开发-初始化目录解析
前端·javascript·vue.js
kk不中嘞27 分钟前
Webpack 核心原理剖析
前端·webpack·node.js
Yvonne爱编码30 分钟前
简述ajax、node.js、webpack、git
前端·git·ajax·webpack·node.js·visual studio
周小码31 分钟前
CesiumJS详解:打造专业级Web 3D地球仪与地图的JavaScript库
前端·javascript·3d