javascript - 在 textarea 光标旁边设置 Bootstrap Popover 位置
问题描述
我正在使用引导弹出窗口向用户显示提示,他们可以在其中从列表中选择提示并将其插入到 textarea 中。他们可以使用按钮打开弹出框,也可以在文本区域中输入“%”字符时打开。弹出框打开良好,并位于弹出框链接(合并字段链接)旁边。但是当用户输入“%”字符时,我希望弹出框在光标旁边打开,而不是在弹出框链接旁边。请看图片
我一直在尝试,但我似乎无法找到一种方法来做到这一点。你们能不能帮我做这件事?我还添加了我的代码。
$(document).ready(function () {
$("[data-toggle=popover]").popover();
$('body').on('click', function (e) {
$('[data-toggle=popover]').each(function () {
// hide any open popovers when the anywhere else in the body is clicked
if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
$(this).popover('hide');
}
});
});
$('#textMsgCandidateOverview').on('keypress', function (e) {
if (e.keyCode === 37) {
debugger;
$('#linkPop').trigger('click');
}
});
$('body').on('click', '#ulMergeField .list-group-item', function () {
debugger
var value = $(this).attr("id");
$(this).parents('.popover').remove();
var target = document.getElementById("textMsgCandidateOverview");
start = target.selectionStart;
if (target.setRangeText) {
//if setRangeText function is supported by current browser
target.setRangeText('%' + value + '%')
target.focus();
target.selectionEnd = start + 2 + value.length;
}
else {
target.focus()
document.execCommand('insertText', false /*no UI*/, '%' + value + '%');
}
});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<div class="form-group row">
<div class="col-md-12">
<a id="linkPop" tabindex="0"
class="btn btn-sm btn-primary"
role="button"
data-html="true"
data-toggle="popover"
data-trigger="click"
data-content="<div class='form-group'><ul id='ulMergeField' class='list-group'><li id='contact_name' class='list-group-item border-0'> Contact name</li><li id='contact_first_name' class='list-group-item border-0'> Contact first name</li><li id='contact_last_name' class='list-group-item border-0'> Contact last name</li><li id='contact_email' class='list-group-item border-0'> Contact email</li><li id='contact_phone' class='list-group-item border-0'> Contact phone</li><li id='contact_address' class='list-group-item border-0'> Contact address</li><li id='agent_name' class='list-group-item border-0'> Agent name</li><li id='agent_first_name' class='list-group-item border-0'> Agent first name</li><li id='agent_last_name' class'list-group-item border-0' Agent last name</li><li id='agent_email' class='list-group-item border-0'> Agent email</li><li id='agent_phone' class='list-group-item border-0'> Agent phone</li></ul></div>">Merge Fields</a>
</div>
</div>
<textarea name="textMsg" id="textMsgCandidateOverview" class="form-control" placeholder="type starting with % for merge fields" required></textarea>
解决方案
推荐阅读
- angular - 在 Angular 中测试 utils 文件。最佳实践是什么?
- javascript - 异步函数的回调函数是否推送到条件递归同步函数中的调用堆栈?
- mysql - 在 MySQL 中替换字符串中的第一个字符
- php - 使用外部 API 进行 Laravel 身份验证
- security - 使用我的连接字符串连接到本地数据库时遇到问题
- swift - Swift - 按日期对从 tableviewSections 中的 db 接收到的对象进行排序
- powerbi - Dax 措施以避免除 Power BI 中的一个之外的所有切片器
- swift - 从类继承协议?
- oracle - Oracle - 连接表上的部分删除
- reactjs - 我如何在函数体中使用通用类型