jQuery中有一些方法可以获取所有嵌套子元素吗?我的意思是从所有嵌套级别完整收集,以便我不需要递归调用函数。如何使用jQuery获取所有嵌套元素?
这里是我的功能
$.fn.extend({
compressElementsWidth: function() {
var range = { min: 9999, max: 0 };
console.log($(this).contents());
$(this).contents().filter(
function() {
if (this.nodeType == 3)
return this.nodeValue && this.nodeValue.replace(/\s{1,6}/,'') ? $(this) : null;
else
return this.nodeName.match(/IMG|A|GROUP|FIELDSET|INPUT|SELECT|TEXTAREA|BUTTON|SUBMIT/) ? $(this) : null;
}).each(function(e) {
var left = $(this).position();
if (p.left < range.min)
range.min = p.left;
var right = p.left + $(this).width;
if (right > range.max)
range.max = right;
});
var max_width = range.max - range.min;
$(this).contents().filter(
function()
{
if (this.nodeType == 3)
return this.nodeValue && this.nodeValue.replace(/\s{1,6}/,'') ? $(this) : null;
else
return this.nodeName.match(/IMG|A|GROUP|FIELDSET|INPUT|SELECT|TEXTAREA|BUTTON|SUBMIT/) ? $(this) : null;
}).each(function(e) {
$(this).css("max-width:", max_width);
});
}
});
window.onload = function() {
$("div.column-right-outer").compressElementsWidth();
};
所以我需要得到内部的所有元素div.column,右外。您可以在this page上测试此代码,只需保存它并包含jQuery和上面的代码即可。例如在博客档案中,有大量的链接列表,我需要获取所有链接和右列下的所有可见文本。如果有图像或表单元素,我也需要它们。
递归和$(节点).find( “*”)的结果是约10.000-16.000和极其缓慢的性能。
你打算如何处理所有这些信息? – ntgCleaner
这是一个函数,它应该计算列(包装器)的“完整内容宽度”。在这个特定的页面中,它并不是完美的例子。但是,如果要删除“热门帖子”列,则应该压缩列的宽度,以便删除多余的宽度。 – user1141649