2014-03-31 24 views
-1

如何請求單個HTML文檔上的所有錨點?jQuery列出頁面上的所有錨點?

$('.notfront article a[href!=http]').attr('href') 

回報只是第一場比賽,

$('.notfront article a').attr('href').match(/^#\w+/g) 

也只返回的第一個元素。

+0

你正在試圖達到什麼目的? –

+2

無論你的選擇是什麼,.attr總是隻返回第一個值。任何jquery getter方法也是如此。沒有關於你想要做什麼的更多信息,除了說你不能這樣做,真的不可能回答這個問題。 –

+0

那麼你只是想返回不是外部超鏈接的第一個錨元素? – bsayegh

回答

2
function getAnchorElements(){ 
    var elems = []; 
    $('.notfront article a').each(function(){ 
     if($(this).attr('href').match(/^#\w+/g)) 
      elems.push(this); 
    }); 
    return elems; 
} 
+0

哦。每個()..我忘了。謝謝! – eapo

1

這應該在沒有foreach循環的情況下選擇它們。

$(".notfront article a[href^='#']"). 
+0

但不這樣做。 – eapo