【发布时间】:2019-09-05 14:22:18
【问题描述】:
我不太喜欢 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>
如您所见,此代码还包含一个 delegate 定义处理特定树节点上的点击的函数,在这里我发现尝试对原始逻辑进行一点更改时遇到了一些困难。
我试图改变的原始逻辑如下:点击一个节点应该意味着点击前面 HTML 代码中的 元素。所以点击它检索的 元素
var pNode = event.target.parentNode.innerText;
pNode = pNode.split(" - ")[0];
此代码应执行以下操作:
获取被点击元素的内容,放入pNode变量。所以 pNode 会包含这样的内容:6.1 - Listino Prezzi(参考前面的 HTML 代码示例),然后拆分并放入仅 pNode 6.1 值。
好的,它工作正常,但由于我必须从我的树可视化中删除这个索引号(6.1 和类似),我必须从其他地方获取这个值。
所以我知道父 元素的 data-value 字段。事实上,正如您在前面的 HTML 示例代码中看到的那样,我有类似的内容:
<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">
如您所见,有 data-value="6.1",它是我必须检索然后在我的函数中使用的值。
正如您在我之前的 JQuery 代码中看到的那样,我尝试以这种方式获取它:
// 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
为什么?怎么了?我错过了什么?如何尝试解决此问题并获取父 元素的 data-value 值的值?
【问题讨论】:
标签: javascript jquery dom-events jstree jquery-events