[英]Get the href attribute for each link in a section of HTML
我編寫了以下jQuery,以遍歷HTML部分中的每個<a>
對象:
$(".chapterindex" + key + " div.link a").each(function(intIndex){
alert("Numbered index: " + intIndex);
});
});
jQuery第一行中使用的鍵值來自我手動構建的URL數組,如下所示:
var chapters = new Array();
chapters[0] = "original/html/0/ID0EFJAE.html";
我可以提醒intIndex
給我0,1,2,3,4,5....
等。
但是如何擴展上面的jQuery,以從HTML中找到的每個鏈接中獲取href
屬性?
嘗試這個:
$(".chapterindex" + key + " div.link a").each(
function(intIndex){
alert( "Numbered index: " + intIndex );
var href = $(this).attr('href');
});
});
您可以通過$(this).attr('href')
訪問它
$(".chapterindex" + key + " div.link a").each(
function(intIndex){
alert( "Numbered index: " + $(this).attr("href") );
});
});
$(".chapterindex" + key + " div.link a").each(function () {
alert(this.href);
});
您是否需要完整的HREF或標簽中顯示的HREF?
有人提出的jQuery對象方法$(this).attr('href')
將返回標記中設置為HREF屬性的任何內容。
John提出的DOM節點屬性方法this.href
將返回完全限定的URL。
因此,給定鏈接<a href="/resources/foo.ext">Foo</a>
,jQuery方法將返回“ /resources/foo.ext”,而另一個方法將返回“ http://mysite.ca” /currentpath/resources/foo.ext”。
因此,這僅取決於您需要退貨的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.