有没有办法在由textarea标签或类似的东西定义的区域中实现自动完成功能?

我正在使用jQuery自动完成插件来向输入标签内的用户建议输入,但问题是自动完成短语通常可能相当长,从而滚动输入字段的边缘。

解决方法

您可以在textareas上使用 jQueryUI autocomplete widget。

一个工作实例:http://output.jsbin.com/cezemetida/1

HTML:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Textarea Autocomplete</title>

    <Meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
      * { padding: 0; margin: 0; }
      html,body { height: 100%; }
      body,textarea {
         font-family: "lucida sans",verdana,arial,helvetica,sans-serif;
         font-size: 80%;
      }
      textarea { font-size: 100%;}
      h1 { font-size: 1.4em; padding: 10px 10px 0; }
      h2 { font-size: 1.2em; padding: 10px 10px 0; color:Navy;}
      p { margin-top: 6px; padding: 0 10px 1em; }

    .ui-widget { font-size: 0.8em; line-height:0.6em; }
    .ui-widget .ui-widget { font-size: 0.7em; }

    .ui-menu-item { font-size: 7pt; }

      #inputDiv {
         left: 1%;
         right: 38%;
      }

      #msgs {
         left: 63%;
         right: 1%;
      }

      div.column {
         border: 1px dotted #666;
         background-color: #E6E6FA;
         position: absolute;
         top: 20px;    /* margin from top */
         bottom: 20px; /* margin from bottom */
         padding: 8px;
      }
      * html  {
         height: 100%;
      }

    </style>


<body>
    <div id='inputDiv' class='column'>
      <h1>jQueryUI Autocomplete demo</h1>
      <p>This demo uses jQueryUI v1.8rc3.</p>
      <p>Type a few characters of a word. Try these: <em>Four,train,even.</em></p>
      <form action="jquery" id="form1">
        <textarea rows='14' cols='80' id="input1"></textarea>

      </form>
    </div>

    <div id='msgs' class='column' style='font-size:10pt;font-weight:normal;color:red;'>
    </div>

  </body>
</html>

JavaScript的:

var phraselist = [
      '"Until he extends the circle of his compassion to all living things,man will not himself find peace."  Albert Schweitzer,early 20th-century German Nobel Peace Prize-winning mission doctor and theologian','First they came for the communists,and I did not speak out—because I was not a communist; Then they came for the socialists,and I did not speak out—because I was not a socialist; Then they came for the Trade unionists,and I did not speak out—because I was not a Trade unionist; Then they came for the Jews,and I did not speak out—because I was not a Jew; Then they came for me—and there was no one left to speak out for me. -- Pastor Martin Niemöller','I and the public kNow // what all schoolchildren learn // Those to whom evil is done // do evil in return.   --W H Auden.','Let us not wallow in the valley of despair,I say to you today,my friends. And so even though we face the difficulties of today and tomorrow,I still have a dream. It is a dream deeply rooted in the American dream. I have a dream that one day this nation will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident,that all men are created equal." I have a dream that one day on the red hills of Georgia,the sons of former slaves and the sons of former slave owners will be able to sit down together at the table of brotherhood. I have a dream that one day even the state of Mississippi,a state sweltering with the heat of injustice,sweltering with the heat of oppression,will be transformed into an oasis of freedom and justice. I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character.  I have a dream today! I have a dream that one day,down in Alabama,with its vicIoUs racists,with its governor having his lips dripping with the words of "interposition" and "nullification" -- one day right there in Alabama little black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers. I have a dream today!','I\'d like to be under the sea in an octopus\' garden,in the shade; He\'d let us in,kNows where we\'ve been -- in his octopus\' garden,in the shade.','Four score and seven years ago our fathers brought forth upon this continent a new nation,conceived in liberty,and dedicated to the proposition that all men are created equal.  Now we are engaged in a great civil war,testing whether that nation,or any nation so conceived and so dedicated,can long endure. We are met here on a great battlefield of that war. We have come to dedicate a portion of it as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But in a larger sense we can not dedicate - we can not consecrate - we can not hallow this ground. The brave men,living and dead,who struggled here,have consecrated it far above our poor power to add or detract. The world will little note,nor long remember,what we say here,but can never forget what they did here. It is for us,the living,rather to be dedicated here to the unfinished work which they have,thus far,so nobly carried on. It is rather for us to be here dedicated to the great task remaining before us that from these honored dead we take increased devotion to that cause for which they here gave the last full measure of devotion - that we here highly resolve that these dead shall not have died in vain; that this nation shall have a new birth of freedom; and that government of the people,by the people,for the people,shall not perish from the earth.','The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed,collaborative,hypermedia information systems. It is a generic,stateless,protocol which can be used for many tasks beyond its use for hypertext,such as name servers and distributed object management systems,through extension of its request methods,error codes and headers [47]. A feature of HTTP is the typing and negotiation of data representation,allowing systems to be built independently of the data being transferred.','What would things have been like [in Russia] if during periods of mass arrests people had not simply sat there,paling with terror at every bang on the downstairs door and at every step on the staircase,but understood they had nothing to lose and had boldly set up in the downstairs hall an ambush of half a dozen people? -- Alexander Solzhenitsyn','The good will is not good because of what it effects or accomplishes or because of its competence to achieve some intended end: it is good only because of its willing (i.e. it is good in itself).  Even if it should happen that,by a particularly unfortunate fate or by the niggardly provision of a step-motherly nature,this will should be wholly lacking in power to accomplish its purpose,and if even the greatest effort should not avail it to achieve anything of its end,and if there remained only the good will-not as a mere wish,but as the summoning of all the means in our power- it will sparkle like a jewel all by itself,as something that had its full worth in itself.','When in the Course of human events,it becomes necessary for one people to dissolve the political bands which have connected them with another,and to assume among the powers of the earth,the separate and equal station to which the Laws of Nature and of Nature\'s God entitle them,a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation. // We hold these truths to be self-evident,that all men are created equal,that they are endowed by their Creator with certain unalienable Rights,that among these are Life,Liberty and the pursuit of Happiness. --That to secure these rights,Governments are instituted among Men,deriving their just powers from the consent of the governed,--That whenever any Form of Government becomes destructive of these ends,it is the Right of the People to alter or to abolish it,and to institute new Government,laying its foundation on such principles and organizing its powers in such form,as to them shall seem most likely to effect their Safety and Happiness. Prudence,indeed,will dictate that Governments long established should not be changed for light and transient causes; and accordingly all experience hath shewn,that mankind are more disposed to suffer,while evils are sufferable,than to right themselves by abolishing the forms to which they are accustomed. But when a long train of abuses and usurpations,pursuing invariably the same Object evinces a design to reduce them under absolute Despotism,it is their right,it is their duty,to throw off such Government,and to provide new Guards for their future security. --Such has been the patient sufferance of these Colonies; and such is Now the necessity which constrains them to alter their former Systems of Government. The history of the present King of Great Britain [George III] is a history of repeated injuries and usurpations,all having in direct object the establishment of an absolute Tyranny over these States. To prove this,let Facts be submitted to a candid world.','The object of a liberal training is not learning,but discipline and the enlightenment of the mind. The educated man is to be discovered by his point of view,by the temper of his mind,by his attitide towards life and his fair way of thinking.  He can see,he can discriminate,he can combine ideas and perceive whither they lead; he has insight and compehension. His mind is a practised instrument of appreciation.  He is more apt to contribute light than heat to a discussion,and will oftener than another show the power of uniting the elements of a difficult subject in a whole view; he has the kNowledge of the world which no one can have who kNows only his own generation or only his own task. // What we should seek to impart in our colleges therefore,is not so much learning itself as the spirit of learning.  You can impart that to young men; and you can impart it to them in the three or four years at your disposal.  It consists in the power to distinguish good reasoning from bad,in the power to digest and interpret evidence,in a habit of catholic observation and a preference for the non-partisan point of view,in an addiction to clear and logical processes of thought and yet an instinctive desire to interpret rather than to stick in the letter of the reasoning,in a taste for kNowledge and a deep respect for the integrity of the human mind.  It is citizenship of the world of kNowledge,but not ownership of it. '
      ];
      phraselist.sort();

      $(document).ready(function() {
          monkeyPatchAutocomplete();

          $("#input1").autocomplete({
              // The source option can be an array of terms.  In this case,if
              // the typed characters appear in any position in a term,then the
              // term is included in the autocomplete list.
              // The source option can also be a function that performs the search,// and calls a response function with the matched entries.
              source: function(req,responseFn) {
                  addMessage("search on: '" + req.term + "'<br/>");
                  var re = $.ui.autocomplete.escapeRegex(req.term);
                  var matcher = new RegExp("\\b" + re,"i" );
                  var a = $.grep( phraselist,function(item,index){
                      //addMessage("&nbsp;&nbsp;sniffing: '" + item + "'<br/>");
                      return matcher.test(item);
                  });
                  addMessage("Result: " + a.length + " items<br/>");
                  responseFn( a );
              },select: function(value,data){
                  var s = ""
                  if (typeof data == "undefined") {
                    s = value;
                  }else {
                    s = data.item.value;
                  }
                  if (s.length > 30) { s = s.substring(0,30) + "..."; }
                  addMessage('You selected: ' + s + "<br/>");
              }
          });
      });

      // This patches the autocomplete render so that
      // matching items have the match portion highlighted.
      function monkeyPatchAutocomplete() {

          // Don't really need to save the old fn,// but I Could chain if I wanted to
          var oldFn = $.ui.autocomplete.prototype._renderItem;

          $.ui.autocomplete.prototype._renderItem = function( ul,item) {
              var re = new RegExp("\\b" + this.term,"i") ;
              var t = item.label.replace(re,"<span style='font-weight:bold;color:Blue;'>" + this.term + "</span>");
              return $( "<li></li>" )
                  .data( "item.autocomplete",item )
                  .append( "<a>" + t + "</a>" )
                  .appendTo( ul );
          };
      }

    function addMessage(msg){
        $('#msgs').append(msg+"<br/>");
    }

jquery – textarea上的自动完成功能[已关闭]的更多相关文章

  1. 使用Html5多媒体实现微信语音功能

    这篇文章主要介绍了使用Html5多媒体实现微信语音功能,需要的朋友可以参考下

  2. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. 处理textarea中的换行和空格

    这篇文章主要介绍了处理textarea中的换行和空格,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  4. html5中canvas图表实现柱状图的示例

    本篇文章主要介绍了html5中canvas图表实现柱状图的示例,本文使用canvas来实现一个图表,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  5. 有关HTML5页面在iPhoneX适配问题

    这篇文章主要介绍了有关HTML5页面在iPhoneX适配问题,需要的朋友可以参考下

  6. Html5 页面适配iPhoneX(就是那么简单)

    这篇文章主要介绍了Html5 页面适配iPhoneX(就是那么简单),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  7. HTML5 移动页面自适应手机屏幕四类方法总结

    这篇文章主要介绍了HTML5 移动页面自适应手机屏幕四类方法总结,非常具有实用价值,需要的朋友可以参考下

  8. H5页面适配iPhoneX(就是那么简单)

    这篇文章主要介绍了H5页面适配iPhoneX(就是那么简单),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  9. 在IOS9中的Cordova应用程序使用JQuery / Javascript的window.history问题

    在两个测试用例中唯一改变的是Cordova.js.解决方法我看到这是几个星期前,但我会发布这个,以防其他人遇到它.听起来它可能与iOS9中的哈希更改生成的导航事件有关.如果是这样,可以将其添加到index.html以禁用哈希侦听:

  10. iOS 5上的jQuery事件

    解决方法在Apple开发论坛上由一个人回答:我需要在将元素添加到DOM之后才绑定(),如下所示:

随机推荐

  1. jquery-plugins – 是否可以使用猫头鹰旋转木马实现循环/无限轮播?

    我正在使用猫头鹰旋转木马,它的工作完美,除了它不支持循环/无限滚动.我没有搜索google和stackoverflow的想法,没有运气.有没有人在猫头鹰旋转木马上实现圆形/无限滚动?

  2. jQuery动态输入字段焦点

    我想使用以下jQuery向我的页面动态添加一个输入字段:在这样做之后,我希望输入字段具有闪烁的文本光标的焦点,所以我想在创建后立即输入.有人可以告诉我我该怎么办?

  3. jquery – 为什么$(window).height()这样错了?

    我试图获取当前浏览器的视口高度,使用但我得到的价值观太低了.当视口高度高达850px时,我从height()获取大约350或400像素的值.这是怎么回事?

  4. jquery – 如果在此div之外和其他draggables内部(使用无效和有效的还原选项),则可拖动恢复

    例如这样但是由于明显的原因,这不行.我可以说这个吗?

  5. 创建一个jQueryUI 1.8按钮菜单

    现在jQueryUI1.8已经出来了,我正在浏览更新,并且遇到了新的Buttonwidget,特别是SplitButtonwithadropdown的演示之一.这个演示似乎表明Buttonwidget可以在这里创建一个下拉菜单.作为讨论的问题,我想知道使用这个新的Button小部件来创建一个下拉菜单有什么方法.干杯.解决方法您必须在按钮下方列出一个列表,方式类似于此处为自动完成提供的演示:http

  6. 灰色divs使用JQuery

    我试图使用这个代码:为了淡出一大堆名为MySelectorDiv的div,唯一的是,它只会淡出第一个而不是所有的div,为什么呢?

  7. 使用jQuery动态插入到列表中

    我有两个订单列表在彼此旁边.当我从一个列表中选出一个节点时,我想按照字母顺序插入到另一个列表中.抓住的是我想要把一个元素放在另一个列表中,而不刷新整个列表.奇怪的是,当我插入到右边的列表中,它工作正常,但是当我插入到左边的列表中时,顺序永远不会出来.我也尝试将所有内容读入数组,并将其排序在一起,以防止children()方法没有按照显示顺序返回任何东西,但是我仍然得到相同的结果.这是我的jQuer

  8. 没有回应MediaWiki API使用jQuery

    我试图从维基百科获取一些内容作为JSON:但我没有回应.如果我粘贴到浏览器的地址栏,就像我得到预期的内容.怎么了?解决方法您需要通过添加&callback=?来触发具有$.getJSON()的JSONP行为?在querystring上,像这样:Youcantestithere.没有使用JSONP,你正在击中same-originpolicy,阻止XmlHttpRequest获取任何数据.

  9. jQuery Ajax请求每30秒

    我有这段代码,但是有些人在我的网站上的值可能会改变.我需要每30秒钟更新一次#financediv.这可以做吗解决方法您可以将代码放在单独的函数中,如下所示:然后每30秒建立一个定时器调用该函数:祝你好运!总结以上是DEVMAX为你收集整理的jQueryAjax请求每30秒全部内容。如果觉得DEVMAX网站内容还不错,欢迎将DEVMAX网站推荐给好友。

  10. jquery – keypress事件在IE和Chrome中不工作,但在FF工作

    任何想法为什么会这样发生?我通常认为Chrome会更加宽容代码?这是我的按键键.我错过了什么吗?右图();和leftimage();是应该工作的功能,因为我在其他地方使用这些功能谢谢您的帮助!

返回
顶部