开发者

selected text and xy coordinates

开发者 https://www.devze.com 2023-01-23 10:46 出处:网络
how to get a selected text and xy coordinates of the开发者_Python百科 word in the same time??Just googled it:

how to get a selected text and xy coordinates of the开发者_Python百科 word in the same time??


Just googled it:

var txt = "";

if (window.getSelection) {
    txt = window.getSelection();
} else if (document.getSelection) {
    // FireFox 
    txt = document.getSelection();
} else if (document.selection) {
    // IE 6/7 
    txt = document.selection.createRange().text;
}

txt = txt.toString()

There is no simple way to get X/Y coordinates of the selected text. Because it dependes on its container position and size, text font, text layout, and many many other variables.


To expand on @MatuDuke's answer, you can get the position of the selected text like so:

var txt = window.getSelection(),
    range = txt.getRangeAt(0),
    boundary = range.getBoundingClientRect();

// Available positions:
// boundary.top
// boundary.bottom
// boundary.left
// boundary.right

These will give you pixel values relative to the viewport. However, it doesn't seem to work within text areas, a problem I'm currently trying to solve.


I am using this jQuery plugin http://plugins.jquery.com/node/7411 for a project and it seems to be working flawlessly. You could use jQuery to get the position of mouse http://docs.jquery.com/Tutorials:Mouse_Position

Here is some sample code I have worked on

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
    <script type="text/javascript" src="jquery.textselect.js"></script>
    <script type="text/javascript">
        $(function(){
            $('#select').bind('textselect click', function(e){
                console.log(e.text);
                console.log(e.pageX);
            })
        });
    </script>
    <!-- Date: 2010-11-05 -->
</head>
<body>
    <div id="select">
        This is a simple select test
    </div>
</body>
</html>


You could try something like this

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
    <script type="text/javascript" src="jquery.textselect.js"></script>
    <script type="text/javascript">
        $(function(){
            $('#select').bind('textselect click', function(e){
                console.log(e.text);
                console.log(e.pageX);

                var selected_text = e.text
                var original_text = $(this).text();
                var parts = original_text.replace(e.text, "/").split("/");

                for(i in parts) {
                    console.log(parts[i])
                }
            })
        });
    </script>
    <!-- Date: 2010-11-05 -->
</head>
<body>
    <div id="select">
        This is a simple select test
    </div>
</body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消