Javascript jstree设置节点href链接和添加onclick事件
我是jQuery和jsTree的新手,我正在尝试完成两件事。1) 我希望节点在单击时转到指定的url。默认情况下,Javascript jstree设置节点href链接和添加onclick事件,javascript,jquery,html,asp.net,jstree,Javascript,Jquery,Html,Asp.net,Jstree,我是jQuery和jsTree的新手,我正在尝试完成两件事。1) 我希望节点在单击时转到指定的url。默认情况下,li具有a标记,其中href=#。2) 我想向某些节点添加onClick事件。我已经搜索过了,但似乎找不到一个明确的答案。我尝试了以下几点: $(function () { $('#leftNav').jstree({ "core": { "themes": { 'name': 'default',
li
具有a
标记,其中href=#
。2) 我想向某些节点添加onClick事件。我已经搜索过了,但似乎找不到一个明确的答案。我尝试了以下几点:
$(function () {
$('#leftNav').jstree({
"core": {
"themes": {
'name': 'default',
"variant": "small",
"icons": false
}
},
"checkbox": {
"keep_selected_style": false
},
});
});
$('#leftNav').on("changed.jstree", function (e, data) {
console.log(data.selected);
var i, j;
for (i = 0, j = data.selected.length; i < j; i++) {
var nodeId = data.selected[i]
nodeId = "#" + nodeId
// 1) Code to change node url
$(nodeId).attr("href", "http://www.google.com/")
// 2) Code to add onClick to node
$(nodeId).click("myFunction")
}
});
为了#1。上面的代码有错误吗
为了#2。为添加事件,然后获取节点ID并重定向-尚未测试的代码可能有错误
为了#1。上面的代码有错误吗
为了#2。为添加事件,然后获取节点ID并重定向-尚未测试的代码可能有错误
如果您只需要1)在url上打开一个新窗口,2)在选择更改时调用一些函数,您可以这样做:
$('#leftNav').on("changed.jstree", function (e, data) {
// open a new window with your url
window.open("http://www.google.com/");
// call a function
myFunction();
})
然而,在这把小提琴中,我保留了每次新选择更改时对所有选定节点的迭代:如果您只需要1)在url上打开一个新窗口,2)在选择更改时调用一些函数,您可以这样做:
$('#leftNav').on("changed.jstree", function (e, data) {
// open a new window with your url
window.open("http://www.google.com/");
// call a function
myFunction();
})
然而,在这把小提琴中,我保留了您在每次新选择更改时对所有选定节点的迭代:我尝试了您提到的解决方案,但在var selectedObj=data.rslt.obj处出现异常;表示未捕获的TypeError:无法读取undefined.do console.log(数据)的属性;在那条线之前。你得到了什么?它显示了一些数据
data=Object{node:Object,selected:Array[1],event:n.event,instance:a.j…e.core}
但是对于data.rslt
它显示为未定义。展开对象,找到id/标题并在if语句中使用它谢谢。我可以通过做这个$(data.node.a_attr.attr(“href”,“www.google.com”)代码>我尝试了您提到的解决方案,但在var selectedObj=data.rslt.obj处出现异常;表示未捕获的TypeError:无法读取undefined.do console.log(数据)的属性;在那条线之前。你得到了什么?它显示了一些数据data=Object{node:Object,selected:Array[1],event:n.event,instance:a.j…e.core}
但是对于data.rslt
它显示为未定义。展开对象,找到id/标题并在if语句中使用它谢谢。我可以通过做这个$(data.node.a_attr.attr(“href”,“www.google.com”)代码>这比我想象的要简单得多!我可以在我的代码var id=data.node.a_attr.id中找到这一点;myFunction(id)代码>谢谢。这比我想象的要简单得多!我可以在我的代码var id=data.node.a_attr.id中找到这一点;myFunction(id)代码>谢谢。
$("#leftNav").bind('select_node.jstree', function(event, data) {
var selectedObj = data.rslt.obj;
//redirect if node matches - console.log(selectedObj); to find the id or title
if(selectedObj.attr("id") == "myNode"){
//redirect or do whatever
windows.location("http://www.google.com");
}
}
$('#leftNav').on("changed.jstree", function (e, data) {
// open a new window with your url
window.open("http://www.google.com/");
// call a function
myFunction();
})