是否可以搜索您开发的所有网页(不只是用户所在的当前页面)以获取具有指定ID的正文标记?如果是这样,我将如何在Javascript中执行此操作?另外,我如何将用户重定向到包含具有唯一ID的主体的页面?
答案 0 :(得分:3)
如果您真的想这样做,如果您知道页面的网址:
,则可以这样做(使用jQuery)
function searchPages(urls,elem,success,failure){
nextUrl(urls,0);
// this calls itself as a callback because $.load is asynchronous
function nextUrl(i){ // this function tries to load a body#myId for the url
$('<div /'>).load(urls[i]+' '+elem,function(){
// if it loaded there'd be html
if($(this).html()!=='') success.call(this,urls[i]);
// if you've loaded all the pages, then you're done
if(i===urls.length)
failure.call(this,urls[i]);
// otherwise, call the function again for the next url
else nextUrl(i+1);
});
};
}
searchPages(['/url1','/url2','/url3'],'body#myId',function(url){
console.log(url); // this is the url you want
console.log($(this)); // $(this) is the div that the body elem was loaded into
},failed);
// this is called if you don't find the page
function failed(){ alert('no page found'); };