Jak uzyskać wszystkie linki do listy wewnątrz div?
Mam kod z następującym drzewem DOM:
<div id="blogPagination">
<div class="pagination">
<ul>
<li>
<a href="/2" >1</a>
</li>
<li>
<a href="/3" >2</a>
</li>
</ul>
</div>
</div>
Próbuję dotrzeć do href mojego tagu. Nie mogę dosięgnąć tego, co próbowałem.
Jaki jest najlepszy sposób na dotarcie do niego za pomocą jQuery?
Próbowałem:
console.log($('#blogPagination div ul > li a ').attr("href"));
console.log($('#blogPagination > a ').attr("href"));
$('#blogPagination').children('a')
console.log($('#blogPagination div ul li a').attr("href"));
bez szczęścia ..
Dzięki
EDYTOWAĆ:
Po odpowiedzi nbrooksa, oto co próbowałem do tej pory:
function bindPagination() {
console.log("bind");
$(function() {
var links = $("#blogPagination ul a").map(function(e) {
e.preventDefault();
return this.href;
}).get();
console.log(links);
});
EDYCJA 2:
Biorąc pod uwagę odpowiedź Syfaro, próbowałem też:
$('#blogPagination').find('a').each(function(e) {
e.preventDefault();
console.log($(this).attr('href'));
});
Bez szczęścia.
EDYCJA 3: Chciałbym podać więcej szczegółów dotyczących tej funkcji, które mogą mieć znaczący wpływ w końcu:
aby załadować tę paginację, używam Ajaxa i kierownic owiniętych w funkcję przygotowania dokumentu:
$(document).ready(function(){
// Get the customer service stats
var Content = {
init: function() {
/* this.getHomePosts(); */
this.getBlogPosts();
},
getBlogPosts: function(offset) {
if(offset == undefined){
offset = 0;
}
// GET the events with JSON
$.ajax({
type: "POST",
data: {},
url: site_url+"/main/blog/"+offset,
dataType: "json",
success: function(results) {
posts = results["posts"].map(function (blogContent) {
if( blogContent.picture != '' ) {
return {
Title: blogContent.title ,
Picture: Content.urlPostPic + blogContent.picture ,
Video: '' ,
Text: blogContent.text ,
Datetime: blogContent.datetime ,
}
} else {
return {
Title: blogContent.title ,
Picture: '' ,
Video: blogContent.video ,
Text: blogContent.text ,
Datetime: blogContent.datetime ,
}
}
});
pagination = {pagination: results["pagination"]};
var template = Handlebars.compile( $('#templateBlog').html() );
$('#blogPosts').append( template(posts) );
var template = Handlebars.compile( $('#templatePagi').html() );
$('#blogPagination').append( template(pagination) );
// Here we call bindPagination <===
bindPagination();
}
});
},
};
Content.init();
Możesz zobaczyć w funkcji get BlogPosts, którą nazywam BindPagination, która ma być tą funkcją, aby zapobiec domyślnemu zachowaniu i wywołać zawartość w zależności od przesunięcia
function bindPagination() {
console.log("bind");
var links = $("#blogPagination ul a").map(function(e) {
e.preventDefault();
return this.href;
}).get();
console.log(links);
$('#blogPagination').find('a').each(function(e) {
console.log("clicked !");
e.preventDefault();
console.log($(this).attr('href'));
// var attr = this.attr();
// var id = attr.replace("/","");
// $('#blogPosts').empty();
// $('#blogPagination').empty();
// Content.getBlogPosts(id);
});
}
});
ostatni }); gotowe do końca dokumentu.