Jquery查找节点并在树视图中高亮显示
我有一个具有Ui Li结构的树视图列表,我想创建一个基于jquery的搜索,它将选择或突出显示树中的文本 有人能帮忙吗? 以下是树状视图示例: 试试这个:Jquery查找节点并在树视图中高亮显示,jquery,search,treeview,Jquery,Search,Treeview,我有一个具有Ui Li结构的树视图列表,我想创建一个基于jquery的搜索,它将选择或突出显示树中的文本 有人能帮忙吗? 以下是树状视图示例: 试试这个: var searchTree: function (textInput) { var count=0; if (textInput === '') { this.find('li:visible').removeClass('search-item-tree'); }
var searchTree: function (textInput) {
var count=0;
if (textInput === '') {
this.find('li:visible').removeClass('search-item-tree');
}
else {
count = this.find('li:visible').removeClass('search-item-tree').filter(function () {
var v = $(this).data();
if (v.name.toUpperCase().indexOf(textInput.toUpperCase().trim()) !== -1) {
$(this).find('[name="basenode"]:first').addClass('search-item-tree');
return true;
}
return false;
}).length;
}
return count;
}
风格:
.search-item-tree{
font-style: italic;
font-weight: bold;
background-color:lightgreen;
}
e、 g:事件更改输入搜索
searchTree.call($('tree selector'),$(this).val())
试试这个:
var searchTree: function (textInput) {
var count=0;
if (textInput === '') {
this.find('li:visible').removeClass('search-item-tree');
}
else {
count = this.find('li:visible').removeClass('search-item-tree').filter(function () {
var v = $(this).data();
if (v.name.toUpperCase().indexOf(textInput.toUpperCase().trim()) !== -1) {
$(this).find('[name="basenode"]:first').addClass('search-item-tree');
return true;
}
return false;
}).length;
}
return count;
}
风格:
.search-item-tree{
font-style: italic;
font-weight: bold;
background-color:lightgreen;
}
e、 g:事件更改输入搜索
searchTree.call($('tree selector'),$(this).val())
在这篇文章中,使用jQuery可以很好地回答您的问题 这里有一个关于你的问题的解决方案张贴在那里
(function (elem, fun) {
$(elem)
.find(":not(iframe)")
.addBack()
.contents()
.filter(function () {
return this.nodeType === 3 && skipSpace(this.nodeValue) && fun(this.parentNode);
});
})("ul:first", function(node) { node.style.color = "red"; });
function skipSpace(str) {
var index = str.search(/^[\S]/);
if (index === -1) {
return "";
}
return str.slice(index);
}
它高亮显示所有非空白的文本元素
我仅使用javascript实现了这一点:
(function searchAndApply(node, fun) {
if(!node) {
return;
}
searchAndApply(node.nextSibling, fun);
searchAndApply(node.firstChild, fun);
if(node.nodeType === 3) {
return skipSpace(node.nodeValue) && fun(node.parentNode);
}
})(document.querySelector("ul:first-child"), function (node) {
node.style.color = "red";
});
function skipSpace(str) {
var index = str.search(/^[\S]/);
if (index === -1) {
return "";
}
return str.slice(index);
}
它的作用完全相同
问候您。使用jQuery在本文中为您的问题提供了一个很好的答案 这里有一个关于你的问题的解决方案张贴在那里
(function (elem, fun) {
$(elem)
.find(":not(iframe)")
.addBack()
.contents()
.filter(function () {
return this.nodeType === 3 && skipSpace(this.nodeValue) && fun(this.parentNode);
});
})("ul:first", function(node) { node.style.color = "red"; });
function skipSpace(str) {
var index = str.search(/^[\S]/);
if (index === -1) {
return "";
}
return str.slice(index);
}
它高亮显示所有非空白的文本元素
我仅使用javascript实现了这一点:
(function searchAndApply(node, fun) {
if(!node) {
return;
}
searchAndApply(node.nextSibling, fun);
searchAndApply(node.firstChild, fun);
if(node.nodeType === 3) {
return skipSpace(node.nodeValue) && fun(node.parentNode);
}
})(document.querySelector("ul:first-child"), function (node) {
node.style.color = "red";
});
function skipSpace(str) {
var index = str.search(/^[\S]/);
if (index === -1) {
return "";
}
return str.slice(index);
}
它的作用完全相同
亲切问候。谢谢这对我有用…有什么方法可以打开树节点吗…就像我在结束节点中搜索00000000 1615一样,我需要添加一个类在找到项时展开,在找不到项时折叠。展开{display:block;}&.collapse{display:none;}以及类“highlight”,同时添加和删除上面的类我们正在向终端节点添加突出显示类…我想要一种向父节点添加类的方法,如上面的鼓架节点。$obj.parent.prev.addClass'someclassName'谢谢这对我有用…是否有一种方法我也可以打开树节点…如我在终端节点中搜索00000000 1615,我需要添加一个类扩展当找到项目时,并在未找到项目时折叠。展开{display:block;}&.collapse{display:none;}以及类“highlight”,同时添加和删除上述类我们正在向端节点添加highlight类…我想要一种向父节点添加类的方法,如鼓架节点上方。$obj.parent.prev.addClass“someclassName”