如何只返回一个数组,其中包含给定链接上的所有链接?
$.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/