gpt4 book ai didi

javascript - 显示用户选择的文本

转载 作者:行者123 更新时间:2023-11-30 00:11:45 24 4
gpt4 key购买 nike

 <script type="text/javascript">
/* run on document load **/
function disp() {
//var text = document.getElementById('TextArea1').value;

var text = document.getElementById('MainContent_TextArea1').value;
//alert('a')

if (text == null)
{
return false;
}
else
{
var t = text;
var t = text.substr(text.selectionStart, text.selectionEnd -text.selectionStart);
document.getElementById('displayval').value = t;
alert(t)
}
}
</script>
<div>
<input id="TextArea1" runat="server" type="text"/>
<INPUT type="button" onclick= "disp()" visible="true" value="Show"/>
<INPUT type="text" id ="displayval" visible="true" />
</div>

这里我试图显示用户选择的文本。但不幸的是在 var t = text.substr(text.selectionStart, text.selectionEnd -text.selectionStart);得到错误。它不工作。此代码中的任何错误。请帮我纠正这个问题..

最佳答案

<script type="text/javascript">
function disp() {
var txtArea = document.getElementById('TextArea1');
var start = txtArea.selectionStart;
var finish = txtArea.selectionEnd;
var sel = txtArea.value.substring(start, finish);
document.getElementById('displayval').value = sel;
alert(sel);
}
</script>
<div>
<input id="TextArea1" runat="server" type="text"/>
<input type="button" onclick= "disp()" visible="true" value="Show"/>
<input type="text" id ="displayval" visible="true" />
</div>

关于javascript - 显示用户选择的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36193479/

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