課程
/前端開發
/JavaScript
/JavaScript進階篇
onselect 中如何提取選中的內容?
如當鼠標在"我就是什么什么的簡介"這段文字中選中"簡介"兩個字時,alert出我選中的"簡介",如果我選中"什么" 就alert 出"什么".
2016-06-07
源自:JavaScript進階篇 6-7
正在回答
你的意思是選中什么就彈出什么吧!可惜我也不會
通過鼠標進行選取復制,但不需要復制這一項,選取就行,他就會提示了
像這樣:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>鼠標取詞</title>
<script>
function getSel()
{
var t=window.getSelection?window.getSelection():(document.getSelection?document.getSelection():(document.selection?document.selection.createRange().text:""))
alert(t)
}
</script></head>
<body >
<textarea name="selectedtext" rows="5" cols="50"onselect="getSel()">以上的代碼可以捕獲到textarea中的內容
</textarea>
</body>
</html>
一句話里每個詞都利用<span>標簽設置onselect事件。
<script type="text/javascript">
function message(){
alert("你讓我跳我就跳");
</script>
這段寫在<head>標簽里
<form>
<textarea name="summary" cols="60" row="5" onselect="message()">我就是什么什么的簡介</textarea>
</form>
這段寫在<body>標簽里
jim_jlu 提問者
舉報
本課程從如何插入JS代碼開始,帶您進入網頁動態交互世界
2 回答內容選中事件(onselect)
1 回答內容選中事件(onselect)對段落可以實現嗎?
8 回答onselect不能觸發文本框選中事件
1 回答如何判斷選中的是復選款
2 回答數組后面如何添加內容?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-09-14
你的意思是選中什么就彈出什么吧!可惜我也不會
2016-08-13
通過鼠標進行選取復制,但不需要復制這一項,選取就行,他就會提示了
2016-06-29
像這樣:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>鼠標取詞</title>
<script>
function getSel()
{
var t=window.getSelection?window.getSelection():(document.getSelection?document.getSelection():(document.selection?document.selection.createRange().text:""))
alert(t)
}
</script></head>
<body >
<textarea name="selectedtext" rows="5" cols="50"onselect="getSel()">以上的代碼可以捕獲到textarea中的內容
</textarea>
</body>
</html>
2016-06-14
一句話里每個詞都利用<span>標簽設置onselect事件。
2016-06-07
<script type="text/javascript">
function message(){
alert("你讓我跳我就跳");
}
</script>
這段寫在<head>標簽里
<form>
<textarea name="summary" cols="60" row="5" onselect="message()">我就是什么什么的簡介</textarea>
</form>
這段寫在<body>標簽里