我如何解析.get的结果,以便只在锚标记中有链接

How do I parse through the results of .get so that I only have the links in the anchor tags?

本文关键字:链接 get 何解析 结果      更新时间:2024-04-19

如何只返回一个数组,其中包含给定链接上的所有链接?

$.get( "http://ocw.mit.edu/courses/biological-engineering/20-010j-introduction-to-bioengineering-be-010j-spring-2006/lecture-notes/", function( data ) {
  alert( "Data Loaded: " + data );
});

您可以使用正则表达式。比如从这个开始

data.match(/<a.+?href=('|")([^'1]+?)'1/mg).map(function(m) { console.log(m) });

正确,不能对外部URL进行直接ajax调用。然而,可以使用YQL和jQuery来实现这一点,如下所示:

HTML-

<ul id="list">
</ul>

JS-

var url = encodeURIComponent("select * from html where url='http://ocw.mit.edu/courses/biological-engineering/20-010j-introduction-to-bioengineering-be-010j-spring-2006/lecture-notes' and xpath='//a/@href'");
var yql = "http://query.yahooapis.com/v1/public/yql?q=" + url + "&format=json";
$.getJSON(yql,
          function(data) {
              $.each(data.query.results.a, function(i, item) {
                  $('#list').append('<li>'+item.href+'</li>');
              });       
          }
);

Fiddle-http://jsfiddle.net/55QV9/