当前位置:首页 > JavaScript

js实现@功能

2026-04-07 01:17:45JavaScript

实现@功能的JavaScript方法

监听输入事件

使用addEventListener监听输入框的inputkeyup事件,检测用户是否输入了@符号。

const inputElement = document.getElementById('commentInput');
inputElement.addEventListener('input', function(event) {
    const text = event.target.value;
    const lastChar = text.slice(-1);

    if (lastChar === '@') {
        showUserList();
    }
});

显示用户列表

当检测到@符号时,显示一个包含用户名的下拉列表供用户选择。

function showUserList() {
    const userList = ['用户1', '用户2', '用户3'];
    const dropdown = document.createElement('div');
    dropdown.className = 'user-dropdown';

    userList.forEach(user => {
        const item = document.createElement('div');
        item.textContent = user;
        item.addEventListener('click', function() {
            insertUsername(user);
            dropdown.remove();
        });
        dropdown.appendChild(item);
    });

    document.body.appendChild(dropdown);
}

插入用户名

用户点击下拉列表中的用户名后,将用户名插入到输入框中@符号之后。

function insertUsername(username) {
    const inputElement = document.getElementById('commentInput');
    const text = inputElement.value;
    const atIndex = text.lastIndexOf('@');

    inputElement.value = text.slice(0, atIndex) + '@' + username + ' ';
    inputElement.focus();
}

样式调整

为下拉列表添加基本样式,确保其显示在输入框下方。

.user-dropdown {
    position: absolute;
    border: 1px solid #ccc;
    background: white;
    max-height: 150px;
    overflow-y: auto;
}

.user-dropdown div {
    padding: 8px;
    cursor: pointer;
}

.user-dropdown div:hover {
    background: #f0f0f0;
}

完整示例

将上述代码整合到一个完整的HTML文件中,确保所有功能正常工作。

js实现@功能

<!DOCTYPE html>
<html>
<head>
    <style>
        .user-dropdown {
            position: absolute;
            border: 1px solid #ccc;
            background: white;
            max-height: 150px;
            overflow-y: auto;
        }
        .user-dropdown div {
            padding: 8px;
            cursor: pointer;
        }
        .user-dropdown div:hover {
            background: #f0f0f0;
        }
    </style>
</head>
<body>
    <textarea id="commentInput" rows="4" cols="50"></textarea>
    <script>
        const inputElement = document.getElementById('commentInput');
        inputElement.addEventListener('input', function(event) {
            const text = event.target.value;
            const lastChar = text.slice(-1);

            if (lastChar === '@') {
                showUserList();
            }
        });

        function showUserList() {
            const userList = ['用户1', '用户2', '用户3'];
            const dropdown = document.createElement('div');
            dropdown.className = 'user-dropdown';

            userList.forEach(user => {
                const item = document.createElement('div');
                item.textContent = user;
                item.addEventListener('click', function() {
                    insertUsername(user);
                    dropdown.remove();
                });
                dropdown.appendChild(item);
            });

            document.body.appendChild(dropdown);
        }

        function insertUsername(username) {
            const inputElement = document.getElementById('commentInput');
            const text = inputElement.value;
            const atIndex = text.lastIndexOf('@');

            inputElement.value = text.slice(0, atIndex) + '@' + username + ' ';
            inputElement.focus();
        }
    </script>
</body>
</html>

注意事项

  • 确保用户列表下拉框的位置正确,可能需要根据输入框的位置动态计算。
  • 考虑添加键盘导航功能,允许用户使用上下箭头选择用户名。
  • 在实际应用中,用户列表可能来自后端API,需使用异步请求获取数据。

标签: 功能js
分享给朋友:

相关文章

vue功能实现

vue功能实现

Vue 功能实现指南 Vue.js 是一个渐进式 JavaScript 框架,广泛用于构建用户界面。以下是 Vue 功能的常见实现方法。 数据绑定 Vue 的核心功能之一是数据绑定,通过 v-mod…

php实现打印功能

php实现打印功能

PHP实现打印功能的方法 在PHP中实现打印功能可以通过多种方式完成,包括直接输出到浏览器、生成PDF文件、调用打印机接口等。以下是几种常见的方法: 直接输出HTML内容 PHP可以通过echo或p…

vue实现倍速播放功能

vue实现倍速播放功能

实现倍速播放功能的方法 在Vue中实现倍速播放功能通常涉及HTML5的<video>元素或第三方播放器库(如video.js)的API调用。以下是具体实现方式: 使用原生HTML5 vi…

vue实现产品使用功能

vue实现产品使用功能

Vue 实现产品功能的关键方法 组件化开发 使用Vue的单文件组件(.vue)将产品功能模块化,每个功能拆分为独立组件。例如产品列表、详情页、购物车等可分别封装为ProductList.vue、Pro…

js实现文件下载

js实现文件下载

使用 a 标签下载文件 通过动态创建 a 标签并设置 download 属性实现文件下载。适用于已知文件 URL 或 Blob 数据的情况。 function downloadFile(url, f…

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置和…