【发布时间】:2011-11-05 03:22:31
【问题描述】:
是否可以获取页面中选定文本的父元素?例如:
<div class="someparent">
Selection of this text should refer to the 'someparent' class.
<span class="spanparent">If this is selected, the parent should be this span</span>
</div>
因为在获取选中文本的时候,一般是从窗口或者文档中获取的(取决于浏览器),但是是否有可能获取选中文本的父元素呢?
【问题讨论】:
-
你能改写一下吗。我无法得到你需要的东西。
-
如果你想在 spanparent 中的文本被选中时获取 div someparent,你可以使用 $('span.spanparent').select(function(){ $(this).parent() ; ... });
-
@Boo:这应该是一个答案:)
-
@Boo:它似乎不起作用,我没有从输入字段中检索文本,我需要获取典型元素(不是输入)上选定文本的父级。跨度>
标签: javascript jquery text