dtjo87679 2010-07-13 05:56
浏览 15
已采纳

使用javascript获取锚标记文本

I don't know if it is possible! Maybe my thought is also wrong. I want to invoke a search corresponding to all links in my Wordpress blog.

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

Thanks.

  • 写回答

2条回答 默认 最新

  • doushui20090526 2010-07-13 05:59
    关注

    Try this:

    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;
    }
    

    What the search function does is it loops through the list of strings and if any have the term in it (indexOf), then it will get pushed into an array which is then returned. If there are no matches, it will return null.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算