0

我有 2 个单独的书签

javascript: (function () { var url = document.location; var links = document.getElementsByTagName('link'); var myvar = 'none'; for (var i = 0, l; l = links[i]; i++) { if (l.getAttribute('rel') == 'next') { myvar = l.getAttribute('href'); break; } } alert(myvar); })();

javascript: (function () { var url = document.location; var links = document.getElementsByTagName('link'); var myvar = 'none'; for (var i = 0, l; l = links[i]; i++) { if (l.getAttribute('rel') == 'prev') { myvar = l.getAttribute('href'); break; } } alert(myvar); })();

我想将它们组合成一个,以便两者都显示在一个书签中。我希望这会做到,但显然我塞满了

javascript: (function () { var url = document.location; var links = document.getElementsByTagName('link'); var myvar = 'none'; var myvar1 = 'none'; for (var i = 0, l; l = links[i]; i++) { if (l.getAttribute('rel') == 'next') { myvar = l.getAttribute('href'); break; } } { if (l.getAttribute('rel') == 'prev') { myvar1 = l.getAttribute('href'); break; } } alert(myvar + ' ' + myvar1); })();
4

1 回答 1

0

试试这个:

javascript: (function () { var url = document.location; var links = document.getElementsByTagName('link'); var prev = 'none', next = 'none'; for (var i = 0, l; l = links[i]; i++) { if (l.getAttribute('rel') == 'prev') { prev = l.getAttribute('href'); } else if(l.getAttribute('rel') == 'next') { next = l.getAttribute('href'); } if(prev != 'none' && next != 'none') { break; }} alert(prev + '\n' + next); })();

此外,这是一个缩小版:

javascript:(function(){for(var e=document.getElementsByTagName("link"),b="none",c="none",d=0,a;(a=e[d])&&!("prev"==a.getAttribute("rel")?b=a.getAttribute("href"):"next"==a.getAttribute("rel")&&(c=a.getAttribute("href")),"none"!=b&&"none"!=c);d++);alert(b+"\n"+c)})();
于 2013-02-17T01:20:30.057 回答