为什么这个简单的javascript/jquery代码无法提醒选定的文本? [英] Why can't this simple javascript/jquery code alert selected text?

查看:65
本文介绍了为什么这个简单的javascript/jquery代码无法提醒选定的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解释以下代码的行为.这是我的整个剧本

I can't explain the behaviour of the code below. Here's my entire script

<html>
<head>
<script type="text/javascript" language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> 
<script type="text/javascript" language="javascript">
var tmpText = '';
$(document).ready(function(){
        tmpText = '';
        $('#btn_bold').click(function(){alert(tmpText);});
        $('textarea').bind('mouseup', function(){
                  tmpText = '';
                  if(window.getSelection){
                    tmpText = window.getSelection();
                  }else if(document.getSelection){
                    tmpText = document.getSelection();
                  }else if(document.selection){
                    tmpText = document.selection.createRange().text;
                  }
                //tmpText = 'hello world';
                alert(tmpText);
        });
});

</script>
</head>
<body>
<button type="button" id="btn_bold">click</button>
<textarea>This is some text</textarea>
</body>
</html>

尝试以下操作:

1)用鼠标突出显示文本区域中的文本.您会注意到javascript会警告您选定的文本.

1) Use your mouse to high light text in the text area. You will notice that javascript alerts you the selected text.

2)按下单击按钮.您会发现javascript会提醒您一个空字符串.

2) Press the click button. You will notice javascript will alert you an empty string.

没有取消注释tmpText = 'hello world';,然后重复上述步骤.这次,您会注意到步骤1)和2)都向您发出"hello world"警报.

No uncomment tmpText = 'hello world'; and repeat the above steps. This time, you'll notice both steps 1) and 2) alerts you "hello world".

在第2步的第一个实验中,为什么没有提醒您与第1步相同的文字?

How come in the first experiment, step 2) does not alert you the same text as step 1)?

我正在使用谷歌浏览器进行测试

推荐答案

因为它不会自动转换为字符串.当您使用alert()直接调用它时,它会在其上运行toString,但是当您分配一个变量以供以后使用时,它将其保留为选择对象,并且当您稍后尝试警告它时,大概不会再次启用该选择(因为您只是单击了按钮).

Because it doesn't automatically get converted to string. When you call it straight with alert(), it runs the toString on it, but when you assign to a variable to be later used, it keeps it as selection object and when you try to alert it later on, you presumably won't have that selection active anymore (because you just clicked the button).

在每个选择的末尾添加toString(),它应该可以正常工作.

Add toString() at the end of each of those selections and it should work as intended.

if(window.getSelection){
                    tmpText = window.getSelection().toString();
                  }else if(document.getSelection){
                    tmpText = document.getSelection().toString();
                  }else if(document.selection){
                    tmpText = document.selection.createRange().text;
                  }

关于jsfiddle的示例

我想在mozilla开发人员页面的getSelection位下对此进行了很好的解释,如果您想更好地解释为什么会这样.

I recall this being explained quite well in the mozilla developer pages under the getSelection bit, if you want a better explanation why it is like this.

mozilla 上找到了指向页面的链接,特别是检查了什么他们在注释"下说.

found the link to the page on mozilla, specifically check what they say under "Notes".

这篇关于为什么这个简单的javascript/jquery代码无法提醒选定的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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