不知道可不可以!也许我的想法也是错误的。我想调用与我的 Wordpress 博客中所有链接对应的搜索。
我正在使用 Ajax 调用来进行此站点中的其他搜索。如何从 html 中的超文本标记中检索链接文本。
例如:<a href='www.example.com'>demo</a>
.here 我想得到 'demo' 作为输入。
谢谢。
最佳答案
试试这个:
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
。
关于php - 使用javascript获取 anchor 标记文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3234528/