[英]How do I get the HTML content between two IDs?
This is my js code: 这是我的js代码:
$("tr[id^=list2ghead_]").each(function(){
j = i+1;
id_name = $(this).attr("id");
id_name_lenght = id_name.length;
if (i >= 10){
id_name = id_name.substr(0, id_name_lenght-2);
}else{
id_name = id_name.substr(0, id_name_lenght-1);
}
html = $("#"+id_name+i).nextUntil("#"+id_name+j).outerHTML;
$("#"+id_name+i).nextUntil("#"+id_name+j).remove();
html = "<div class='content'>"+html+"</div>";
$(html).insertAfter("#"+id_name+i);
i++;
});
I want to get the HTML content between two IDs. 我想获取两个ID之间的HTML内容。 After that I want to add to this HTML to a div
with a class "content", remove the exiting HTML, and insert after an ID the HTML the new div
. 之后,我想将此HTML添加到带有“内容”类的div
,删除现有的HTML,然后在ID后面的HTML中插入新的div
。 Now outerHtml
returns undefined
. 现在, outerHtml
返回undefined
。 What should I do ? 我该怎么办 ?
This is htnml code generated by jQgrid plugin: 这是jQgrid插件生成的htnml代码:
.outerHTML
is not a property of a jQuery selection. .outerHTML
不是jQuery选择的属性。
Here is a way to move a selection from one place to another : 这是一种将选择从一个地方移动到另一个地方的方法:
var $div = $('<div class="content"></div>');
var $selection = $("#"+id_name+i).nextUntil("#"+id_name+j);
$div.append($selection).insertAfter("#"+id_name+i);
You could use jQuery and Javascript outerHTML property. 您可以使用jQuery和Javascript的externalHTML属性。 If both nodes are at the same level: 如果两个节点处于同一级别:
var theHtml = "";
var id1 = "beginId";
var id2 = "endId";
var $node = $("#" + id1);
var nodes = new Array();
while($node.attr("id") != id2)
{
nodes.push($node);
theHtml += $node.get(0).outerHTML;
$node = $node.next();
}
nodes.push($node); // Edit: also add the second node to the list
theHtml += $node.get(0).outerHTML; // Get the HTML of node with id2
$.each(nodes, function() {
$(this).remove();
});
$(".content").attr("id","theHtml").html(theHtml);
This will include the html of both nodes with id1 and id2. 这将包括id1和id2的两个节点的html。
jQuery object doesn't have outerHTML property. jQuery对象没有externalHTML属性。 For getting the property you can use the prop method. 要获取属性,可以使用prop方法。 $collection.prop('outerHTML')
. $collection.prop('outerHTML')
。 Note that prop
as getter returns value of the first matched element, so you should iterate through the collection and read the properties individually. 请注意, prop
作为getter返回第一个匹配元素的值,因此您应该遍历集合并分别读取属性。
var outerHTML = $("#"+id_name+i).nextUntil("#"+id_name+j).map(function() {
return this.outerHTML;
}).get().join('');
Note that div
element can't have tr
children! 注意div
元素不能有tr
子元素!
You could also get the next element using indexes and eq
method: 您还可以使用索引和eq
方法获取下一个元素:
var $col = $("tr[id^=list2ghead_]");
$col.each(function(i) {
var $next = $col.eq(i + 1);
var $between = $(this).nextUntil($next); //.filter('.jqrow');
var outerHTML = $between.map(function() {
return this.outerHTML;
}).get().join('');
console.log(outerHTML);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.