Questions about $., .parent(node) and nodes in javascript
The following is a part of jstree, http://www.jstree.com/ code.
And I have a couple of questions about this javascript/jquery to ask. (actually pretty much all)
$('#pages-wrapper').tree({
callback:{
"onchange":function(node,tree){
document.location='pages.php?action=edit&id='+node.id.replace(/.*_/,'');
},
"onmove":function(node){
var p=$.tree.focused().parent(node);
var new_order=[],nodes=node.parentNode.childNodes;
for(var i=0;i<nodes.length;++i)new_order.push(nodes[i].id.replace(/.*_/,''));
$.getJSON('/ww.admin/pages/move_page.php?id='+node.id.replace(/.*_/,'')+
'&parent_id='+(p==-1?0:p[0].id.replace(/.*_/,''))+'&order='+new_order);
}
Q1. What $ in var p=$.tree is doing?
Q2. What is .parent(node) referencing to?
Q3. Is this line, var new_order=[],nodes=node.parentNode.childNodes; declearing two variables at the same time? And what is node.parentNode.childNodes doing?
Thanks in advance.
The followings are additional codes.
html is the following
<div id="pages-wrapper">
<ul>
<li id="page_24">
<a href="pages.php?id=24"><ins> </ins>Home</a>
</li>
<li id="page_25">
<a href="pages.php?id=25"><ins> </ins>Second Page</a>
</li>
<li id="page_30"><a href="pages.php?id=30"><ins> </ins>Information</a>
<ul>
<li id="page_31"><a href="pages.php?id=31"><ins> </ins>Illustration</a></li>
</ul>
</li>
....
....
move_page.php
require '../admin_libs.php';// this one does all the db queries
$id=(int)$_REQUEST['id'];
$to=(int)$_REQUEST['parent_id'];
$order=explode(',',$_REQUEST['order']);
dbQuery("update pages set parent=$to where id=$id");
for($i=0;$i<count($order);++$i){
$pid=(int)$order[$i];
dbQuery("update pages set ord=$i where id=$pid");
echo "update pages set ord=$i where 开发者_如何学Cid=$pid\n";
}
What $ in var p=$.tree is doing?
$
is a helper object used by the popular javascript frameworks jQuery, prototype etc.
$.tree
is referring to a extension tree
which is defined somewhere.
What is .parent(node) referencing to?
Looks like it finds the parent node for of the currently selected node, to know the parentID which is passed to the php script. (You will need to let us know what the tree framework you are using...)
Is this line, var new_order=[],nodes=node.parentNode.childNodes; declearing two variables at the same time?
Yes, it is declaring 2 seperate variable on the same line which is legel Javascript.
And what is node.parentNode.childNodes doing?
It gets the list of the sibling elements for the current node selected.
精彩评论