【问题标题】:jquery text input change on button clickjquery文本输入改变按钮点击
【发布时间】:2011-08-31 08:08:36
【问题描述】:

我在这个站点和其他站点中使用了几个线程来修复 ie 的 selectionStart 和 selectionEnd,并决定从这个 site 实现代码。

在简单的 html 页面中,有多个具有公共类和唯一 ID 的文本字段。有几个按钮可以用作虚拟键盘。专注于任何文本文件并单击按钮会将文本放入该元素的光标位置。如果选择了一些在 Mozilla 和 chrome 中也可以正常工作的文本。但是其他浏览器有问题

opera - 起搏文本很好,但在选择时替换文本 添加到选择端(即,如果从右到左进行选择,则从左到左放置,反之亦然)。

ie8 - 行的无效参数 `stored_range.moveToElementText(元素);

整个代码如下所示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>selection test</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script type='text/javascript' src='js/jquery-1.4.2.js'></script>
<script>

window.onload = function () {

 jQuery('.iptext').live('focus', function(event) {
            holdFocus=$(this);
            holdFocus=holdFocus[0];//this is required
        });
 jQuery('.addchar').live('click', function(event) {
            // Get focused textfield
            if(holdFocus==null)
            {

            }
            else
            {
            var element =holdFocus;
if( document.selection ){
    // The current selection
    var range = document.selection.createRange();
    // We'll use this as a 'dummy'
    var stored_range = range.duplicate();
    // Select all text
    stored_range.moveToElementText( element );
    // Now move 'dummy' end point to end point of original range
    stored_range.setEndPoint( 'EndToEnd', range );
    // Now we can calculate start and end points
    element.selectionStart = stored_range.text.length - range.text.length;
    element.selectionEnd = element.selectionStart + range.text.length;
}
                    $(holdFocus).val(
                    $(holdFocus).val().substring(0, holdFocus.selectionStart)+
                    $.trim($(this).text())+
                    $(holdFocus).val().substring(holdFocus.selectionEnd)
            );
            }
        });
};
</script>
<style type="text/css" media="screen">
body { background-color: #fff; font: 16px Helvetica, Arial; color: #000; }
ul {display:block;}
li { list-style: none;display:inline-block;width:20px;height:20px;background-color:#808000;}
ul li a {display:block;text-decoration:none; }
</style>
</head>
<body>
   <input type="text" id="txt1" class="iptext" autocomplete="off" value="Testing String">
   <input type="text" id="txt2" class="iptext" autocomplete="off" value="Testing String">
<ul>
<li><a  href="#" class="addchar">a</a></li>
<li><a href="#" class="addchar">b </a></li>
<li><a  href="#" class="addchar">c</a></li>
<li><a  href="#" class="addchar">d </a></li>

</ul>


</body>
</html>

这是一个小提琴link

【问题讨论】:

    标签: javascript jquery internet-explorer selection textfield


    【解决方案1】:

    您选择从中获取的网站代码并非万无一失。此外,在 IE 中,您通常需要在处理插入符号或选择位置之前聚焦输入(使用其focus() 方法)。

    我推荐my own jQuery plug-in 处理文本输入和文本区域选择。这是我所知道的唯一在 IE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      相关资源
      最近更新 更多