[英]rewrite jquery so that it targets only parent ul li
Jsfiddle: http://jsfiddle.net/X4uvD/ Jsfiddle: http : //jsfiddle.net/X4uvD/
I'm trying to make this jquery code rewrite only parent ul li and not ul li inside. 我正在尝试使此jquery代码仅重写父ul li而不重写ul li内部。
Any ideas how to achieve this? 任何想法如何实现这一目标?
//This is for footer slider, it rewrites 1 ul into several uls that contain 4 li max.
// get the container, useful for later too...
var container = $(".fproductslides");
// get all available UL and LI elements...
// this doesn't order featured listings at the top of the list
// var li_elements = container.find("LI").clone();
// this does order featured listings at the top of the list
var li_elements = container.find("LI.slidefeatured").clone();
li_elements = li_elements.add(container.find('LI:not(.slidefeatured)').clone());
// remove the current content so that we can rebuild it for the slider...
container.find("UL").remove();
// build the slider container...
var slide_container = $("<div />");
slide_container.addClass("slides_container");
// tricky part: looping through the LI's and building each of the slides...
// first create some helpful variables...
var li_elements_per_slide = 3;
var li_counter = 0;
// create the first slide, with a UL to hold the LI's...
var current_li_div = $("<div />");
current_li_div.append($("<ul />"));
// loop through the LI's...
li_elements.each(function(index, element){
li_counter++;
var current_li = $(element).clone();
current_li_div.find("UL").append(current_li);
if (li_counter % li_elements_per_slide == 0)
{
// we've hit 4 in this list, so add the slide and make
// a new one, using same code as before...
container.append(current_li_div);
current_li_div = $("<div />");
current_li_div.append($("<ul />"));
}
});
// we might have an uneven number of LI's, so we need to check for this...
if (li_counter % li_elements_per_slide != 0)
container.append(current_li_div);
You can use parent()
method to get parent DOM elements. 您可以使用
parent()
方法获取父DOM元素。 also you can pass a selector string like ul.parent
to filter parent element. 您也可以传递选择器字符串(如
ul.parent
来过滤父元素。
<ul class='parent'>
<li class='child'></li>
</ul>
$('li.child').parent('ul.parent')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.