Get anchor tag text using javascript

I do not know if this is possible! Maybe my thought is wrong too. I want to trigger a search matching all the links in my Wordpress blog.

I am using the Ajax call for my other search on this site. How to get linking text from a hypertext tag in html. eg <a href='www.example.com'>demo</a>. I want to get " demo " as input.

Thank.

+3
source share
2 answers

Try the following:

var links_html_list = [];

var links = document.getElementsByTagName('a');

for(var l in links) {
   if(typeof links[i] == undefined) continue;
   links_html_list.push(links[i].innerHTML);
}

function search(term) {
   var results = [];
   for(var l in links_html_list) {
       var cur = links_html_list[l];
       if(typeof cur == undefined) continue; 
       if(cur.indexOf(term) != -1) results.push(cur);
   }
   return (results.length > 0) ? results : null;
}

search, , , term (indexOf), , . , null.

+4

$homePageText = file_get_contents(file.html);
preg_match_all('/<a .*?>(.*?)<\/a>/',$homePageText,$matches);

$matches.

+1

Source: https://habr.com/ru/post/1754352/


All Articles