使用javascript从html中搜索文本

Search for a text from html using javascript

本文关键字:搜索 文本 html javascript 使用      更新时间:2023-09-26

我是javascript的新手,尝试在外部html页面中搜索文本。我试图直接在浏览器上实现这一点,因此nodejs不是一个选项。

我知道这不是在SE上问的那种问题,但我别无选择。到目前为止,即使在互联网上广泛搜索也无法解决我的问题。任何指针都可能有帮助。

我认为在客户端没有简单的方法可以做到这一点,这是服务器端的典型工作。出于安全原因,浏览器将阻止此类脚本。

是的,它可以完成,您应该使用PHP Simple HTML DOM Parser

包括此http://simplehtmldom.sourceforge.net/

PHP

//find.php
    <?php
    $url = $_REQUEST['url'];
    $strtofind = $_REQUEST['strtofind'];
    $cont = file_get_html($url)->plaintext; 
    $pos = strpos($cont , $strtofind );
    echo ($pos==false?"Not found":"Found");
?>

这是JS函数

//js function
$.get("find.php?url="+url+"&strtofind="+strtofind, function(data){
console.log(data);
});

将您想要的任何URL传递给paas。JS将发出请求,PHP文件将提供源代码。