Как получить значения из одного класса в jquery?
<div class="wider-box google-map-wrapper">
<h3 class="touch-line uc">Product tags</h3>
<div class="wider-box content-block profile-tags">
<p>
<a href="/directory/product/electronic_goods/">Electronic Goods</a>
<a href="/directory/product/home_appliances/">Home Appliances</a>
</p>
</div>
<h3 class="touch-line uc">Brand tags </h3>
<div class="wider-box content-block profile-tags">
<p>
<a href="/directory/brand/apple/">Apple</a>
<a href="/directory/brand/breville/">Breville</a>
<a href="/directory/brand/de_longhi/">De Longhi</a>
<a href="/directory/brand/dyson/">Dyson</a>
<a href="/directory/brand/kenwood/">Kenwood</a>
</p>
</div>
Мне нужны данные в href. Это должно быть
Electronic Goods | Home Appliances
а также
Apple | Breville | De Longhi | Dyson | Kenwood.
Оба должны быть разными.
Использование map():
var str = $('a').map(function(){
return $.trim(this.innerHTML)
}).get().join(' | ');
Удалите join(), чтобы получить массив.
Вы можете сделать это:
var data = [];
$('.wider-box p').each(function () {
data.push($(this).children().map(function () {
return $(this).text();
}).get());
});
Вот демо: http://jsfiddle.net/wared/59FXP/.
вы можете добавить некоторый класс в свой тег anhour, тогда вы можете иметь его в другом объекте
menu1 for all menu 1s
<a class='menu1' href="/directory/product/electronic_goods/">Electronic Goods</a>
menu2 for all menu 2s
<a class='menu2' href="/directory/brand/apple/">Apple</a>
см. скрипку.
Попробуйте использовать каждую функцию и получите текст <a>
$('div.profile-tags a').each(function(){
var allTexts=[];
allTexts.push($(this).text());
});
попробуй что-нибудь вроде этого
var all_list=[];
$('div.profile-tags').each(function(){
var text_list = []
$(this).find('a').each(function(){
text_list.push($(this).text());
})
all_list.push(text_list);
});
console.log(all_list[0].join('|'));
console.log(all_list[1].join('|'));
Прилагается к @A. Ответ Wolff, если вы хотите, чтобы оба класса привязывали текст отдельно, используйте следующее:
$(document).ready(function() {
$( ".wider-box" ).each(function() {
var str = $(this).find("a").map(function(){
return $.trim(this.innerHTML)
}).get().join(' | ');
alert(str);
});
});
Демо здесь: http://jsfiddle.net/PqxRR/5/