简体   繁体   English

单击“使用jQuery”选择/取消选择“文本”

[英]Select/Unselect Text on click Using jQuery

I want to create the following behavior in IE9: 我想在IE9中创建以下行为:

Clicking on the textbox will select the text from the textbox. 单击文本框将从文本框中选择文本。 Clicking on it again will unselect the text. 再次单击它将取消选择文本。

I tried the following from this linK: http://www.codingforums.com/showthread.php?t=105530 我尝试了以下内容: http//www.codingforums.com/showthread.php? t = 105530

var x = 2;

function selectIt(obj)
{
    if (x % 2 == 0)
    {
        obj.select();
    }
    else
    {
        if (document.selection)
        {
            document.selection.empty();
            obj.blur();
        }
        else
        {
            window.getSelection().removeAllRanges();
        }
    }
    obj.focus();
    x++;
}

I also used this: http://jsfiddle.net/HmQxZ/1/ 我也用过这个: http//jsfiddle.net/HmQxZ/1/

But the above solutions have weird behaviors when applied to several textboxes. 但是当应用于多个文本框时,上述解决方案具有奇怪的行为。 What is the best way to approach this kind of problem. 解决此类问题的最佳方法是什么? Is it possible to do this without using a global variable? 是否可以在不使用全局变量的情况下执行此操作?

UPDATE: 更新:

The fiddle works in Chrome. 小提琴适用于Chrome。 But it does not work in IE9. 但它在IE9中不起作用。 In IE9, the text is selected but when you click on the textbox again, the text is not unselected/unhighlighted. 在IE9中,文本被选中,但是当您再次单击文本框时,文本不会被取消选中/不突出显示。 In Chrome, the second click unselects/unhighlights the text. 在Chrome中,第二次单击取消选择/取消突出显示文本。

Thank you. 谢谢。

The problem with several text boxes would be that your x variable is global. 几个文本框的问题是你的x变量是全局的。 You'd need a separate x variable per textbox. 每个文本框需要一个单独的x变量。

You could use a map: 你可以使用地图:

var x = {};

function selectIt(obj)
{
    var key = ... <-- get name (or id) of textbox from obj somehow to use as key in map
    if (!x.hasOwnProperty(key)) x[key] = 0;
    if (x[key] % 2 == 0)
    {
        obj.select();
    }
    else
    {
        if (document.selection)
        {
            document.selection.empty();
            obj.blur();
        }
        else
        {
            window.getSelection().removeAllRanges();
        }
    }
    obj.focus();
    x[key]++;
}

This works for me in Chrome - there is a toggle event function in jQuery but it is not needed in this case 这适用于我在Chrome中 - 在jQuery中有一个切换事件函数,但在这种情况下不需要它

$('input').click(function() {
 // the select() function on the DOM element will do what you want
 this.select();
});

​ but I suggest you tell the script which types of fields you want to select 但我建议您告诉脚本您要选择哪些类型的字段

$("input[type=text], input[type=url]").click(function() {
  $(this).select(); // "this" is native JS 
});

DEMO DEMO

Try this Demo 试试这个演示

DEMO jQuery: 演示jQuery:

$(function(){
    $("input[type='Text']").on("click",function(){
     if (typeof this.selectionStart == "number") 
         this.select();
    });
  });

Here is your complete solution. 这是您的完整解决方案。

Demo http://codebins.com/bin/4ldqp79 演示 http://codebins.com/bin/4ldqp79

HTML HTML

<div id="panel">
  <input type="text" value="Click Me to Select Text" />
  <input type="text" value="Click Me to Select Text" />
  <input type="text" value="Click Me to Select Text" />
  <input type="text" value="Click Me to Select Text" />
  <input type="text" value="Click Me to Select Text" />
  <input type="text" value="Click Me to Select Text" />
</div>

JQuery JQuery的

$(function() {
    $("#panel input[type=text]").click(function() {
        $(this).select();
    });
});

CSS CSS

input{
  display:block;
  border:1px solid #333;
  background:#efefef;
  margin-top:15px;
  padding:3px;
}

Demo http://codebins.com/bin/4ldqp79 演示 http://codebins.com/bin/4ldqp79

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM