js交互
代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
<title>Document</title>
<style>
.upCenter {
width: 100%;
overflow: hidden;
}
.upCenter ul {
width: 100%;
overflow: hidden;
padding: 20px 10px;
box-sizing: border-box;
}
.upCenter ul li {
width: 100%;
overflow: hidden;
padding: 10.5px 0;
font-size: 14px;
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
}
.upCenter ul li span {
display: inline-block;
}
.upCenter ul li span.upSpan {
width: 160px;
display: inline-block;
color: black;
font-weight: normal;
margin: 0;
padding: 0;
}
.upCenter ul li p.upCP {
display: inline-block;
}
.upCenter ul li dl {
display: inline-block;
display: flex;
flex-direction: row;
justify-content: flex-start;
}
.upCenter ul li dl dt {
margin-right: 10px;
padding: 4px 8px;
background: #ff9939;
border-radius: 2px;
cursor: pointer;
}
.input-textarea {
height: 78%;
width: 99%;
resize: none;
border: 1px solid rgb(193, 222, 250);
border-radius: 4px;
padding: 5px;
outline-color: rgba(83, 160, 231, 0.61); // 鼠标聚焦边框颜色
}
.upText {
width: 600px;
}
</style>
</head>
<body>
<div class="upCenter">
<ul>
<li><span class="upSpan">关键词:</span>
<textarea name="" id="keyWords" class="upText input-textarea"></textarea>
</li>
<li><span class="upSpan"></span>
<dl>
<dt onclick="keyWord(1)">+</dt>
<dt onclick="keyWord(2)">|</dt>
<dt onclick="keyWord(3)">(</dt>
<dt onclick="keyWord(4)">)</dt>
</dl>
</li>
</ul>
</div>
<script>
// 关键词操作
function keyWord (val) {
let keyWrodInput = $("#keyWords").val();
let textInput = document.getElementById('keyWords');
let insert = textInput.selectionStart;
if (val == 1) {
textInput.value = textInput.value.substr(0, insert) + ' + ' + textInput.value.substr(insert);
} else if (val == 2) {
textInput.value = textInput.value.substr(0, insert) + ' | ' + textInput.value.substr(insert);
} else if (val == 3) {
textInput.value = textInput.value.substr(0, insert) + ' ( ' + textInput.value.substr(insert);
} else if (val == 4) {
textInput.value = textInput.value.substr(0, insert) + ' ) ' + textInput.value.substr(insert);
}
textInput.focus();
setTimeout(function () {
textInput.setSelectionRange(insert + 3, insert + 3);
}, 10);
}
</script>
</body>
</html>