【发布时间】:2015-05-20 12:54:12
【问题描述】:
每当我点击复选框时,它包含任何子“UL”,这个“LI”是最后一个,然后,我需要复制这个 UL 子只有“SPAN”需要复制并粘贴到目标文件夹.我尝试了下面的代码。
目标:每当我按下它包含任何子 ul 的复选框时,如果它是最后一个,那么,它应该被复制。
在这个列表上,如果我点击Node 1.1.1', Node 1.1.2 etc,那么它应该只复制节点1.1.1、节点1.1.2等
在这个列表中,如果我点击 Node 1.1,那么它应该只复制节点 1.1.1、节点 1.1.2 等。
在这个列表上,如果我点击 Node 1.2, Node 1.3 etc[Here It does not have descendant],那么它应该只复制节点 1.2、节点 1.3 等。而不是“节点 1.1”,因为它有后代。
在这个列表上,如果我点击,'Node 1',那么,它应该复制
Node 1.1.1, Node 1.1.2, Node 1.1.3, Node 1.1.4 and Node 1.1.5 然后
Node 1.2[not 1.1 because it has descendant], Node 1.3, Node 1.4, Node 1.5 and Node 1.6
有什么想法吗?请。
<div class="form-group floatleft10" id="tree">
<ul>
<li>
<input type="checkbox" value="Node 1" /><span>Node 1</span>
<ul>
<li>
<input type="checkbox" value="Node 1.1" /><span>Node 1.1</span>
<ul>
<li>
<input type="checkbox" value="Node 1.1.1" /><span>Node 1.1.1</span></li>
<li>
<input type="checkbox" value="Node 1.1.2" /><span>Node 1.1.2</span></li>
<li>
<input type="checkbox" value="Node 1.1.3" /><span>Node 1.1.3</span></li>
<li>
<input type="checkbox" value="Node 1.1.4" /><span>Node 1.1.4</span></li>
<li>
<input type="checkbox" value="Node 1.1.5" /><span>Node 1.1.5</span></li>
</ul>
</li>
<li>
<input type="checkbox" value="Node 1.2" /><span>Node 1.2</span>
</li>
<li>
<input type="checkbox" value="Node 1.3" /><span>Node 1.3</span>
</li>
<li>
<input type="checkbox" value="Node 1.4" /><span>Node 1.4</span>
</li>
<li>
<input type="checkbox" value="Node 1.5" /><span>Node 1.5</span>
</li>
<li>
<input type="checkbox" value="Node 1.6" /><span>Node 1.6</span>
</li>
</ul>
</li>
<li>
<input type="checkbox" value="Node 2" /><span>Node 2</span>
<ul>
<li>
<input type="checkbox" value="Node 2.1" /><span>Node 2.1</span>
</li>
<li>
<input type="checkbox" value="Node 2.2" /><span>Node 2.2</span>
</li>
</ul>
</li>
</ul>
</div>
<div class="form-group floatleft13" id="checkbox-content"></div>
jQuery
(function ($) {
$('div#tree input[type="checkbox"]').click(function () {
var $list = $("div#checkbox-content");
var $currElem = $(this).clone();
var $currElemNext = $(this).next().clone();
var $curElemVal = $(this).attr("value");
var $curElemValAdd = "";
var $curElemValNext = $(this).next().text();
if ($(this).parent().children("ul").children("li").prop('tagName') == undefined && $(this).parent().children("ul").prop('tagName') == undefined) {
console.log($(this).val());
$list.append($('<div data-value="' + $(this).val() + '"></div>').append($currElemNext));
var $thisTempAdd = $(this).val();
var $currElemSiblngs = $(this).parent().clone();
//$currElemSiblngs.find(":checkbox").remove();
var newCopiedElements = $currElemSiblngs.find("span.daredevel-tree-label").clone();
var $tempVal = $(this).html();
//$curElemValAdd += '<div class="try"><span>'+ $tempVal +'</span></div>';
console.log("Here");
$list.find("div").filter(function () { return $(this).data('value') === $thisTempAdd });
}
/// Second Level
else if ($(this).parent().children("ul").children("li").prop('tagName') == "LI" && $(this).parent().children("ul").prop('tagName') == "UL") {
//console.log();
var $currParent = $(this).parent().prop('tagName');
if ($(this).parent().has('ul') && $(this).parent().children('ul').parent().prop('tagName') == $currParent) {
console.log("dshdsdhdd");
var $currElemSiblngs = $(this).parent().children('ul').children("li");
//$currElemSiblngs.find(":checkbox").remove();
var newCopiedElements = $currElemSiblngs.find("span.daredevel-tree-label").clone();
var $curElemValAdd = "";
$(newCopiedElements).each(function () {
var $tempVal = $(this).html();
console.log($(this).text());
$curElemValAdd += '<div data-value="' + $(this).text() + '"><span>' + $tempVal + '</span></div>';
});
$list.append($curElemValAdd);
}
}
/// First Level
else if ($(this).parent().children("ul").children("li").children("ul").children("li").prop('tagName') == "LI") {
var $currParent = $(this).parent().prop('tagName');
if ($(this).parent().children("ul").children("li").children("ul") && $(this).parent().children("ul").children("li").children("ul").children("li").prop('tagName') == $currParent) {
console.log("dshdsdhddHHHHHHHHHHH");
var $currElemSiblngs = $(this).parent().children("ul").children("li").children("ul");
//$currElemSiblngs.find(":checkbox").remove();
var newCopiedElements = $currElemSiblngs.find("span.daredevel-tree-label").clone();
var $curElemValAdd = "";
$(newCopiedElements).each(function () {
var $tempVal = $(this).html();
console.log($(this).text());
$curElemValAdd += '<div data-value="' + $(this).text() + '"><span>' + $tempVal + '</span></div>';
});
$list.append($curElemValAdd);
}
} else if (!this.checked) {
//alert("new function");
var thisTemp = $(this).val();
//console.log(thisTemp);
$list.find("div").filter(function () { return $(this).data('value') === thisTemp }).remove();
}
else if (this.checked) {
$list.append($('<div></div>').append($currElemNext));
//$list.append($currElemNext);
} else {
//hide to the right
$list.find('span').filter(function () {
console.log($curElemVal);
console.log($curElemValAdd);
return ($(this).text() === $curElemVal || $(this).text() === $curElemValAdd)
}).slideUp("fast", function () {
//$(this).next('span').remove();
$(this).remove();
//alert("2");
});
}
});
})(jQuery);
【问题讨论】:
-
你能试着更清楚地表达自己吗?预期的行为是什么?请为社区提供一些用例!这是一个小提琴:jsfiddle.net/167bqchz
-
@bernland,是的,我在这里表达了我的观点jsfiddle.net/167bqchz/1
标签: javascript jquery html