Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Javascript在p标记内搜索_Javascript_Html_Search_Filter - Fatal编程技术网

如何使用Javascript在p标记内搜索

如何使用Javascript在p标记内搜索,javascript,html,search,filter,Javascript,Html,Search,Filter,我想用纯javascript搜索,但我不知道如何搜索。我试过用谷歌搜索它,也搜索过stackOverflow,但我不知道如何在中搜索 <input id="search"> <p>Some text content I want</p> <script> //script to search the above "p" </script> 我想要一些文本内容 //用于搜索上述“p”的脚本 我希望javascript为它找到的匹

我想用纯javascript搜索
,但我不知道如何搜索。我试过用谷歌搜索它,也搜索过stackOverflow,但我不知道如何在
中搜索

<input id="search">
<p>Some text content I want</p>
<script>
  //script to search the above "p"
</script>

我想要一些文本内容

//用于搜索上述“p”的脚本
我希望javascript为它找到的匹配项设置样式

这是我所得到的一份报告

摘要:

  • 您键入一个查询
  • JS正在寻找匹配项
    • 如果找到匹配项:突出显示匹配项

您可以这样做:

function myfunction(ctrl) {
var TextInsideP = ctrl.getElementsByTagName('p')[0].innerHTML;
}

您的
p
标记内容现在位于变量
TextInsideP
内,您可以随意使用它们。

您可以先获取#search元素中第一个p元素的内容,然后执行搜索。将返回搜索元素的字符串位置,如果未找到,则返回-1

var text = document.querySelector('#search p')[0].innerHTML;
var n = text.search("text to find");

if (n !== -1) {
  // Found!
} else {
  // Not found
}

如果您正在搜索“p”标记中的特定单词,则必须使用选择器抓取它。然后,您必须拆分字符串,循环遍历它们,并使用indexOf进行搜索(见下文)

var input=document.getElementById('search');
input.addEventListener('keyup',function(){
var val=输入值;
搜索匹配(val);
searchPartialMatch(val);
})
var p=document.getElementById('test');
var text=p.innerHTML;
var splits=text.split(“”);
函数searchExactMatch(值){
var字=值;
var索引=拆分索引(word);
如果(索引>-1){
log('与:'完全匹配,拆分[索引]);
}
}
函数searchPartialMatch(值){
var字=值;
对于(var t=0;t-1){
log('键入的字符串与:'部分匹配,拆分);
}
}
}

文本位于此处


您可以使用
document.getElementsBytagName(“标记名”)
访问打开和关闭标记之间的文本


从这一点可以看出,在所有情况下,自然错误都会引起人们的赞誉,因为所有人都有自己的权利,他们都是发明家和准建筑师。不,不,不,不,不,不,不,不,不,不,不,我们的劳动和财富是我们的财富。但是最低限度的成本,是否需要在实验室中进行实际操作,是否需要从消费品中获得流动性?你是说,你是谁,你是谁,你是谁,你是谁,你是谁,你是谁,你是谁

试试看 函数myFunction(){ //document.write(document.getElementsByTagName(“p”)[0].innerText);//标记中的旧文本 document.getElementsByTagName(“p”)[0].innerText=“这很好用。”;//标记内的新文本。 }
我认为如果你想突出显示内容,那么你需要用另一个内容替换一些内容。 我的想法如下:让你的搜索词加粗:

let paragraph = document.getElementById('idHere').innerHTML;
document.getElementById('idHere').innerHTML = paragraph.split('yourSearchWord').join('<b>yourSearchWord</b>');
let paragration=document.getElementById('idHere').innerHTML;
document.getElementById('idHere').innerHTML=段落.split('yourSearchWord').join('yourSearchWord');

我编写了一个函数,当输入中键入的值与
p
元素中的字符串完全匹配时,该函数将为您突出显示
p
元素

var elements=document.querySelectorAll('p');
var HIGHLIGHT_STYLE_PREFIX='';
var HIGHLIGHT_STYLE_SUFFIX='';
document.getElementById('search')。addEventListener('input',highlightMatches);
函数highlightMatches(){
var queryTerm=this.value.trim();
for(设i=0;i-1){
元素[i].innerHTML=elements[i].innerHTML.replace(突出显示样式前缀“”).replace(突出显示样式后缀“”);
}
}
});
}
}

清晰可见
全向性

是一种自然错误,它是一种赞美,是一种自我保护,是一种发明者的真实和准建筑学,这一点是明确的。 Nemo enim ipsam voluptatem,quia voluptas sit,aspernatur aut odit aut fugit,sed quia

奥姆尼斯

根据实际情况,按比例分配

这是一个很好的例子,一个很好的例子,一个很好的例子,一个很好的例子 沃鲁帕特姆。但是最低限度的最低成本,是否需要进行实践

是否需要使用实验室的物质,是否需要使用商品的流动性?我们必须为自己辩护,为自己辩护,为自己辩护,为自己辩护 消费品、照明品、酒杯、酒杯?

我找到了一个感谢所有回答我问题的人

    $(function() {

  // the input field
  var $input = $("input[type='search']"),
    // clear button
    $clearBtn = $("button[data-search='clear']"),
    // prev button
    $prevBtn = $("button[data-search='prev']"),
    // next button
    $nextBtn = $("button[data-search='next']"),
    // the context where to search
    $content = $(".content"),
    // jQuery object to save <mark> elements
    $results,
    // the class that will be appended to the current
    // focused element
    currentClass = "current",
    // top offset for the jump (the search bar)
    offsetTop = 50,
    // the current index of the focused element
    currentIndex = 0;

  /**
   * Jumps to the element matching the currentIndex
   */
  function jumpTo() {
    if ($results.length) {
      var position,
        $current = $results.eq(currentIndex);
      $results.removeClass(currentClass);
      if ($current.length) {
        $current.addClass(currentClass);
        position = $current.offset().top - offsetTop;
        window.scrollTo(0, position);
      }
    }
  }

  /**
   * Searches for the entered keyword in the
   * specified context on input
   */
  $input.on("input", function() {
    var searchVal = this.value;
    $content.unmark({
      done: function() {
        $content.mark(searchVal, {
          separateWordSearch: true,
          done: function() {
            $results = $content.find("mark");
            currentIndex = 0;
            jumpTo();
          }
        });
      }
    });
  });

  /**
   * Clears the search
   */
  $clearBtn.on("click", function() {
    $content.unmark();
    $input.val("").focus();
  });

  /**
   * Next and previous search jump to
   */
  $nextBtn.add($prevBtn).on("click", function() {
    if ($results.length) {
      currentIndex += $(this).is($prevBtn) ? -1 : 1;
      if (currentIndex < 0) {
        currentIndex = $results.length - 1;
      }
      if (currentIndex > $results.length - 1) {
        currentIndex = 0;
      }
      jumpTo();
    }
  });
});
$(函数(){
//输入字段
var$input=$(“input[type='search']),
//清除按钮
$clearBtn=$(“按钮[data search='clear']),
//上一个按钮
$prevBtn=$(“按钮[data search='prev']),
//下一个按钮
$nextBtn=$(“按钮[data search='next']),
//要搜索的上下文
$content=$(“.content”),
//jQ