JavaScript查找并突出显示文本搜索按钮不起作用

时间:2016-12-03 23:03:47

标签: javascript

我正在使用下面的脚本查找并突出显示页面上的文字。如果您输入文本并按回车键,它会很完美,但奇怪的是,如果您单击“查找”按钮它将无效,除非您首先单击页面上的某个位置。就像搜索框处于焦点或突出显示时一样,“查找”按钮将不起作用,即使它将使用Enter键在焦点上工作。我尝试在CSS中删除焦点,但没有运气。

<script type="text/javascript">   
        var TRange=null;

        function findString (str) {
         if (parseInt(navigator.appVersion)<4) return;
         var strFound;
         if (window.find) {

          // CODE FOR BROWSERS THAT SUPPORT window.find

          strFound=self.find(str);
          if (!strFound) {
           strFound=self.find(str,0,1);
           while (self.find(str,0,1)) continue;
          }
         }
         else if (navigator.appName.indexOf("Microsoft")!=-1) {

          // EXPLORER-SPECIFIC CODE

          if (TRange!=null) {
           TRange.collapse(false);
           strFound=TRange.findText(str);
           if (strFound) TRange.select();
          }
          if (TRange==null || strFound==0) {
           TRange=self.document.body.createTextRange();
           strFound=TRange.findText(str);
           if (strFound) TRange.select();
          }
         }
         else if (navigator.appName=="Opera") {
          alert ("Opera browsers not supported, sorry...")
          return;
         }
         if (!strFound) alert ("String '"+str+"' not found!")
         return;
        }
    </script>

这是搜索表单

<form id="f1" name="f1" action="javascript:void()" onsubmit="if(this.t1.value!=null &amp;&amp; this.t1.value!='') parent.findString(this.t1.value);return false;">
    <input type="text" id="t1" name="t1" value="text" size="20">
    <input type="submit" name="b1" value="Find">
</form>

我在http://www.javascripter.net/faq/searchin.htm

找到了该脚本

2 个答案:

答案 0 :(得分:2)

用此替换您的表单代码(在按钮上添加onclick方法):

x = int(input("Pick between 1,2,3,4,5: "))

if x == 1:
    print("You picked 1")
elif x == 2:
    print("You picked 2")
elif x == 3:
    print("You picked 3")
elif x == 4:
    print("You picked 4")
elif x == 5:
    print("You picked 5")
else:
    print("This is not a valid input, please try again")
    #Want to go back to asking the start question again

答案 1 :(得分:0)

按钮工作正常,只需清理你的html和脚本(jsfiddle):

<form id="f1" name="f1" action="javascript:void(0)" onsubmit="
findString(this.t1.value);return false;">

    <input type="text" id="t1" name="t1" value="text" size="20">
    <input type="submit" name="b1" value="Find">
</form>

和javascript:

 var TRange=null;

        window.findString = function(str) {
        alert(str);
        if (parseInt(navigator.appVersion)<4) return;
         var strFound;
         if (window.find) {

          // CODE FOR BROWSERS THAT SUPPORT window.find

          strFound=self.find(str);
          if (!strFound) {
           strFound=self.find(str,0,1);
           while (self.find(str,0,1)) continue;
          }
         }
         else if (navigator.appName.indexOf("Microsoft")!=-1) {

          // EXPLORER-SPECIFIC CODE

          if (TRange!=null) {
           TRange.collapse(false);
           strFound=TRange.findText(str);
           if (strFound) TRange.select();
          }
          if (TRange==null || strFound==0) {
           TRange=self.document.body.createTextRange();
           strFound=TRange.findText(str);
           if (strFound) TRange.select();
          }
         }
         else if (navigator.appName=="Opera") {
          alert ("Opera browsers not supported, sorry...")
          return;
         }
         if (!strFound) alert ("String '"+str+"' not found!")
         return;
        }