jquery捕获单词值 [英] jquery capture the word value

查看:89
本文介绍了jquery捕获单词值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用jquery或javascript捕获单词值?在示例中:



搜索:五月份的行情



当我点击搜索:或行情或任何字眼,我提醒那个单词文字?



更新:



这就是我的意思:



http:// jsfiddle.net/BE68L/



有没有办法可以获取文本值而不将其包装到某个html元素中?

解决方案

要查找较长段落中的特定单词,您必须创建范围对象 TextRange的用于查找段落中特定单词的坐标。一旦映射了每个单词的坐标,就可以将窗口单击事件坐标与该范围内的单词进行匹配。



检索DOM文本节点位置的问题与你所追求的有一些相似之处。



总之,我认为准确得到被点击的单词应该是完全可行的,即使是在较长的段落中,但它肯定不是非常容易,对于较长的文本,最终可能会非常效率低下



对于mozilla / webkit浏览器,您可以这样做:

  $(文件).ready(function(){
$(body)。click(function(){
var s = window.getSelection();
s.modify('extend' ,'后退','单词');
var b = s.toString();

s.modify('extend','forward','word');
var a = s.toString();
s.modify('move','forward','character');
aler t(b + a);

});
});

示例:
http://jsfiddle.net/niklasvh/rD2uE/



它不适用于IE<但是,您可能需要使用IE选择来执行不同类型的解决方案。


Is there a way to capture word value with jquery or javascript? in the example :

Search: Quotes of the Month for May

When I click "Search:" or "Quotes" or any word, that I alert that word text?

Update :

This is what I meant :

http://jsfiddle.net/BE68L/

Is there a way I can get text value without wrapping it into some html element?

解决方案

To find specific words in longer paragraphs you would have to create Range objects and TextRange's to find the coordinates of specific words in the paragraph. Once you have mapped the coordinates for each word, you could then match the window click event coordinates to the word that is within the range.

The question at Retrieving DOM textnode position has some similarities of what you are after.

In short, I think it should be perfectly doable to accurately get the word that is clicked, even in longer paragraphs, but it certainly isn't very easy, and for longer pieces of texts, it may end up being very inefficient.

edit for mozilla/webkit browsers, you could do it like this:

$(document).ready(function() {
    $("body").click(function(){
         var s = window.getSelection();
        s.modify('extend','backward','word');        
        var b = s.toString();

        s.modify('extend','forward','word');
        var a = s.toString();
        s.modify('move','forward','character');
         alert(b+a);

    });
});

example: http://jsfiddle.net/niklasvh/rD2uE/

It doesn't work on IE <= 8 though, you'd probably need to do a different kind of solution using the IE selections.

这篇关于jquery捕获单词值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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