我不太喜欢JavaScript和JQuery,我发现以下困难。
在页面中重新绘制了一棵树(使用JQuery**JStree*库)。
因此,在我的视图代码中,我有类似这样的内容,表示这棵树的一个节点:
<li role="treeitem" data-jstree="{"opened":true,"selected":true,"icon":"/_layouts/15/images/ArxeiaProtocollo/Default/Titolario/tag.png"}" data-id="1" data-value="6.1" aria-selected="true" aria-level="3" aria-labelledby="j1_5_anchor" aria-expanded="true" id="j1_5" class="jstree-node jstree-open">
<i class="jstree-icon jstree-ocl" role="presentation"/>
<a class="jstree-anchor jstree-clicked" href="#" tabindex="-1" id="j1_5_anchor">
<i class="jstree-icon jstree-themeicon jstree-themeicon-custom" role="presentation" style="background-image: url("/_layouts/15/images/ArxeiaProtocollo/Default/Titolario/tag.png"); background-position: center center; background-size: auto;"/>6.1 - Listino Prezzi</a>
<ul role="group" class="jstree-children">
<li role="treeitem" data-jstree="{"opened":false,"icon":"/_layouts/15/images/ArxeiaProtocollo/Default/Titolario/tag.png"}" data-id="1" data-value="6.1.1" aria-selected="false" aria-level="4" aria-labelledby="j1_6_anchor" id="j1_6" class="jstree-node jstree-leaf">
<i class="jstree-icon jstree-ocl" role="presentation"/>
<a class="jstree-anchor" href="#" tabindex="-1" id="j1_6_anchor">
<i class="jstree-icon jstree-themeicon jstree-themeicon-custom" role="presentation" style="background-image: url("/_layouts/15/images/ArxeiaProtocollo/Default/Titolario/tag.png"); background-position: center center; background-size: auto;"/>6.1.1 - Acquisti - AAA</a>
</li>
<li role="treeitem" data-jstree="{"opened":false,"icon":"/_layouts/15/images/ArxeiaProtocollo/Default/Titolario/tag.png"}" data-id="0" data-value="6.1.0" aria-selected="false" aria-level="4" aria-labelledby="j1_7_anchor" id="j1_7" class="jstree-node jstree-leaf jstree-last">
<i class="jstree-icon jstree-ocl" role="presentation"/>
<a class="jstree-anchor" href="#" tabindex="-1" id="j1_7_anchor">
<i class="jstree-icon jstree-themeicon jstree-themeicon-custom" role="presentation" style="background-image: url("/_layouts/15/images/ArxeiaProtocollo/Default/Titolario/tag.png"); background-position: center center; background-size: auto;"/>6.1.0 - Acquisti - ZZZ</a>
</li>
</ul>
</li>
这是使用JSTree JQuery库呈现树的脚本(它工作得非常好):
<script>
$(function () {
$(#tree).jstree(
{
"search" : {
"show_only_matches" : false
},
"plugins" : ["search"]
}
}).delegate("a", "click", function (event, data) {
var pNode = event.target.parentNode.innerText;
pNode = pNode.split(" - ")[0];
// MY NEW CODE START
var pNodeDataValue = event.target.parentNode.data('value');
alert(pNodeDataValue);
// MY NEW CODE END
window.location.replace('" + link + "' + pNode); });
var to = false;
$('#tree_q').keyup(function () {
if(to) { clearTimeout(to); }
to = setTimeout(function () {
var v = $('#tree_q').val();
$('#tree').jstree(true).search(v);
}, 250);
});
});
</script>
如您所见,此代码还包含
代表
定义一个函数来处理对特定树节点的点击,在这里我发现在原始逻辑上实现一个小的改变有些困难。
我试图改变的原始逻辑如下:单击节点应该意味着单击
元素在前面的HTML代码中。因此,点击
它检索的元素
var pNode = event.target.parentNode.innerText;
pNode = pNode.split(" - ")[0];
此代码应执行以下操作:
pNode
pNode
will包含如下内容:
6.1-Listino Prezzi
(参考前面的HTML代码示例),然后拆分并放入
pNode
6.1
价值
所以我知道
数据值
<li role="treeitem" data-jstree="{"opened":true,"selected":true,"icon":"/_layouts/15/images/ArxeiaProtocollo/Default/Titolario/tag.png"}" data-id="1" data-value="6.1" aria-selected="true" aria-level="3" aria-labelledby="j1_5_anchor" aria-expanded="true" id="j1_5" class="jstree-node jstree-open">
正如你所看到的那样
数据值=“6.1”
它是我必须检索并在函数中使用的值。
// MY NEW CODE START
var pNodeDataValue = event.target.parentNode.data('value');
alert(pNodeDataValue);
// MY NEW CODE END
TypeError: event.target.parentNode.data is not a function
为什么?怎么了?我错过了什么?如何尝试修复此问题并获取
父母的
要素