selectionStart-End with textareas [英] selectionStart-End with textareas

查看:193
本文介绍了selectionStart-End with textareas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个恼人的问题,我似乎无法在textarea中获取所选文本的起始和结束索引,我得到的所有内容都是这样的:

I'm having this annoying problem, I can't seem to get the starting and ending index of the selected text in a textarea, all I get is undefined like this:

$('#myarea').selectionStart; // return undefined

我做错了什么?

推荐答案

尝试:

$('#myarea')[0].selectionStart;

为什么? jQuery选择器不返回实际的DOM元素,而是返回包装的jQuery集合。 jQuery使实际的DOM元素可以作为一个数组访问,所以如果你想使用第一个匹配的元素(在这种情况下,只有一个,因为它是由ID),你可以做上面的事情。

Why? A jQuery selector does not return the actual DOM elements but the wrapped jQuery collection. jQuery makes the actual DOM elements accessible as an array, so if you wanted to use the 1st matched element (and in this case, the only one, since it's by ID), you would do the above.

这篇关于selectionStart-End with textareas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆