admin管理员组文章数量:1180511
I am trying to loop through all elements in a given div and output the results (C# code i will use later) to the screen for testing.
so if i have html like this:
<div id="testDiv">
<test>
<a>aVal</a>
<c>
<cc>ccVal</cc>
</c>
</test>
</div>
i am trying to produce this string value:
HtmlElement.CreateNode("test").AddNode(CreateNode("a").addText("aVal")).AddNode(CreateNode("c").AddNode(CreateNode("cc").addText("ccVal"))
Right now i ahve this jquery in place, but i am unsure of how to drill down into the other nodes:
var x = "HtmlElement.";
$('div#testDiv').children().each(function () {
var nodeNameStr = this.nodeName.toLowerCase();
var nodeText = $(this).text();
x += "CreateNode(nodeNameStr).addText(nodeText)"
});
I am trying to loop through all elements in a given div and output the results (C# code i will use later) to the screen for testing.
so if i have html like this:
<div id="testDiv">
<test>
<a>aVal</a>
<c>
<cc>ccVal</cc>
</c>
</test>
</div>
i am trying to produce this string value:
HtmlElement.CreateNode("test").AddNode(CreateNode("a").addText("aVal")).AddNode(CreateNode("c").AddNode(CreateNode("cc").addText("ccVal"))
Right now i ahve this jquery in place, but i am unsure of how to drill down into the other nodes:
var x = "HtmlElement.";
$('div#testDiv').children().each(function () {
var nodeNameStr = this.nodeName.toLowerCase();
var nodeText = $(this).text();
x += "CreateNode(nodeNameStr).addText(nodeText)"
});
Share
Improve this question
edited Aug 9, 2012 at 0:40
driangle
11.8k6 gold badges49 silver badges56 bronze badges
asked Aug 8, 2012 at 23:39
some_bloody_foolsome_bloody_fool
4,68514 gold badges40 silver badges46 bronze badges
4 Answers
Reset to default 20jsFiddle Example
$('#testDiv').find('*').each(function() {
// do stuff
});
Here's a more complete example than previous answers:
http://jsfiddle.net/4QtS5/
// returns the 'AddNode(...)' method call for every child.
function addChildren(element){
var command = "";
$(element).find("> *").each(function(){
command += ".AddNode("+createNode(this)+")";
});
return command;
}
// if the element has text, add the text
function addText(element){
var elementText = $(element).clone().children().remove().end().text().trim();
if(elementText) {
return ".addText(\""+elementText+"\")";
} else {
return "";
}
}
// returns the 'CreateNode(...)' method call for a node and all its children.
function createNode(element){
var nodeName = element.nodeName.toLowerCase();
var csharpCommand = "CreateNode(\""+nodeName+"\")";
csharpCommand += addChildren(element);
csharpCommand += addText(element);
return csharpCommand;
}
// begin
$("div#testDiv > *").each(function(){
var csharpCommand = "HtmlElement."+createNode(this);
console.log(csharpCommand);
});
You can use the div id to get all the children in the following way:
$('#youDivId').children().each(function(){
alert(this.value);
});
You are looping through the direct children of your div, rather than all the children. To do so, use this code:
$('div#testDiv *').each(function(){
// Your Code
});
本文标签: javascriptJQuery How to loop through all child elements of a divStack Overflow
版权声明:本文标题:javascript - JQuery: How to loop through all child elements of a div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738122102a2064909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论