gpt4 book ai didi

javascript - 选择没有最后一个空格的潜水文本

转载 作者:行者123 更新时间:2023-11-27 23:38:15 25 4
gpt4 key购买 nike

我需要在鼠标点击时选择 div 内容(只有文本没有最后一个空格)

<div>
<div id="block1">
Please send the following code
</div>
<div id="block2" style="font-size:40px; background-color:#fffb95;" onclick="selectText('block2')" >
@Html.DisplayFor(c => c.GeneratedCode, new { id = "genCode" })
</div>
<div id="block3">
to your vendor to generate an access code which is required for your authentication.
</div>
</div>

脚本

function selectText(containerid) {
if (document.selection) { // IE
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));

window.getSelection().removeAllRanges();
window.getSelection().addRange(range );
}
}

风格

#block1, #block2, #block3 {
display: inline;
}

.input-lg {
height: 43px;
}

这是选择前的输出 here当我选择 here我想从选择中删除最后一个空格

最佳答案

你是说这样? - 我只是把生成的代码放在一个span里,然后我把style属性放到span上。

   function selectText(containerid) {
if (document.selection) { // IE
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));
range.cloneRange();
window.getSelection().removeAllRanges();
window.getSelection().addRange(range );
}
}
#block1, #block2, #block3 {
display: inline;
}

.input-lg {
height: 43px;
}
<div>
<div id="block1">
Please send the following code
</div>
<div id="block2" onclick="selectText('block2')">
<span style="font-size:40px; background-color:#fffb95;">2256</span>
</div>
<div id="block3">
to your vendor to generate an access code which is required for your authentication.
</div>
</div>

关于javascript - 选择没有最后一个空格的潜水文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57146787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com