[英]Selecting all elements with some class and parent with a specific id
In the HTML below I'm trying to create a list with all innerHTML
of <h3>
but the condition is that the parent div has to be with the id I passed to it. 在下面的HTML中,我试图用<h3>
所有innerHTML
创建一个列表,但条件是父div必须与我传递给它的id相同。
<div id="site_somesite.com" class="wrapperSite">
<h2 class="siteName">somesite.com</h2>
<div class="placementWrapper">
<h3 class="placementName" title="above_01"> above_comments_01</h3>
</div>
<div class="placementWrapper">
<h3 class="placementName" title="above_02"> above_comments_02</h3>
</div>
</div>
<div id="site_anothersite.com" class="wrapperSite">
<h2 class="siteName">anothersite.com</h2>
<div class="placementWrapper">
<h3 class="placementName" title=below_01"> above_comments_01</h3>
</div>
<div class="placementWrapper">
<h3 class="placementName" title="below_02"> above_comments_02</h3>
</div>
</div>
and this is the piece of jQuery code I have so far: 这是到目前为止我拥有的jQuery代码:
$('.sitePick').click(function(event){
var placements = ['<ul>'];
$('.wrapperSite').css("display", "none");
var site = event.target.hash.replace(/\./, "\\."); // get the id
console.log(site);
$(site).show();
var filter = site.replace('#', '').replace(/\\/g,"").trim(); //
$('.placementName').each(function(){
placements += '<li>' + $.trim(this.innerHTML) + '</li>';
})
placements += '</ul>';
});
In this function filter holds the id I want to use but I don't know how to use it to select all elements with the class placementName
. 在这个函数过滤器容留我想使用的ID,但我不知道如何使用它来选择与类的所有元素placementName
。 To make it a bit more clear...how can I create a list with all <h3>
under the div with the id="site_anothersite.com"
? 为了使它更加清楚...我如何在div下使用id="site_anothersite.com"
创建所有<h3>
的列表?
Thanks! 谢谢!
You need to modify your code to: 您需要将代码修改为:
var filter = site.replace('#', '').replace(/\\/g,"").trim(); //
$('#'+filter+' .placementName').each(function(){
placements += '<li>' + $.trim(this.innerHTML) + '</li>';
});
or 要么
Use .site object and finds .placementName
elements in it: 使用.site对象并在其中找到.placementName
元素:
$(site).find('.placementName').each(function(){
placements += '<li>' + $.trim(this.innerHTML) + '</li>';
});
how can I create a list with all
<h3>
如何创建所有<h3>
It should be: 它应该是:
$('.sitePick').click(function(event){
var placements = $('<ul>'); // <-----create object here
$('.wrapperSite').css("display", "none");
var site = event.target.hash.replace(/\./, "\\."); // get the id
console.log(site);
$('#'+site).show(); // <-----------i guess you need a id selector here
var filter = site.replace('#', '').replace(/\\/g,"").trim(); //
$('#'+filter).find('.placementWrapper .placementName').each(function(){ // loop over here
placements.append('<li>' + $.trim(this.innerHTML) + '</li>');
});
$('#'+filter).find('.placementWrapper').append(placements); //<---put it here
});
yes you have to use site object as it is working and find h3 object then grab the value like this: 是的,您必须使用站点对象,因为它正在运行并找到h3对象,然后获取像这样的值:
$('document').ready(function() {
$('.sitePick').click(function(event){
var placements = ['<ul>'];
$('.wrapperSite').css("display", "none");
var site = event.target.hash.replace(/\./, "\\."); // get the id
console.log(site);
$(site).show();
$(site).find("h3").each(function(){
placements += '<li>' + $.trim(this.innerHTML) + '</li>';
});
placements += '</ul>';
console.log (placements);
});
});
your html has typos: 您的html有错别字:
<div id="site_somesite.com" class="wrapperSite">
<h2 class="siteName">somesite.com</h2>
<div class="placementWrapper">
<h3 class="placementName" title="above_01"> above_comments_01</h3>
</div>
<div class="placementWrapper">
<h3 class="placementName" title="above_02"> above_comments_02</h3>
</div>
</div>
<div id="site_anothersite.com" class="wrapperSite">
<h2 class="siteName">anothersite.com</h2>
<div class="placementWrapper">
<h3 class="placementName" title="below_01"> above_comments_01</h3>
</div>
<div class="placementWrapper">
<h3 class="placementName" title="below_02"> above_comments_02</h3>
</div>
</div>
<a class="sitePick" href="#site_anothersite.com" >click to target site_anothersite.com</a>
Jsfiddle : https://jsfiddle.net/kodedsoft/tu7cmqqz/3/ Jsfiddle: https ://jsfiddle.net/kodedsoft/tu7cmqqz/3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.