繁体   English   中英

如何在Javascript DOM中创建树结构?

[英]How to create a tree structure in Javascript DOM?

这听起来太愚蠢/太基本了-对此感到抱歉,但是我做了很多尝试,无法找到正确的解决方案!

我正在使用以下这段代码-使一行文本字段上下移动。 不幸的是,上下运动并没有停止并且在整个页面上都进行! :)

function up(row) {
    // need to stop somewhere
    var prevRow = row.previousSibling;
    if(prevRow != null) {
        row.parentNode.insertBefore(row, prevRow);
    }
};

function down(row) {
    // need to stop somewhere as well
    var nextRow = row.nextSibling;
    row.parentNode.insertBefore(row, nextRow.nextSibling);
};

我生成的html是xml和xsl的组合。 XSL看起来像这样:

<xsl:for-each select=".../...">
<p>
<button type="button" onclick="up(this.parentNode)">Up</button>
<button type="button" onclick="down(this.parentNode)">Down</button>
<input>
...
...
...
</p>
</xsl:for-each>

如上所述,这是可行的,但上下运动不会停止。 我尝试将xsl:for-each封装在另一个p标签和div标签中,但都没有用。 我试图让这些p标签的父元素作为body标签以外的东西。

我说清楚了吗?


生成的HTML添加如下:

<html>
<head>
<script>
function up(row) {
...
};
function down(row) {
...
};
</script>
</head>
<body>
<form name="edit_form" action="edit.php" method="POST">
...
<?xml version="1.0"?>
...
<p>
<button type="button" onclick="up(this.parentNode)">Up</button>
<button type="button" onclick="down(this.parentNode)">Down</button>
<input name="CRorder[record10354881]" type="text" value="0" disabled="" size="4"/>
<input name="CRpreference[record10354881]" type="text" value="10" disabled="" size="4"/>
<input name="CRlabel[record10354881]" type="text" value="label1"/><input name="CRvalue[record10354881]" type="text" value="22222222222"/></p>
<p><button type="button" onclick="up(this.parentNode)">Up</button>
<button type="button" onclick="down(this.parentNode)">Down</button>
<input name="CRorder[record10354882]" type="text" value="1" disabled="" size="4"/>
...
...
</form></body>
</html>

根据您的HTML,并假设... s不包含使P成为子对象的匹配标记对,则包含Up / Down按钮(和其他副词)的P将在Ps列表中向上移动,直到FORM标签的第一个孩子。 由于它直接与BODY标签相邻,因此实际上是将其一直沿页面向上移动。

编辑:好的,根据您的评论,如果您没有将P标记的其他同级移到其他位置,则需要以某种方式对其进行标记,并更改上/下功能以遵守这些限制。 就像是...

...<tag id="upperLimit">...

function up(row) {
  // need to stop somewhere
   var prevRow = row.previousSibling;

   if(prevRow != null && prevRow != document.getElementById("upperLimit")) {
     row.parentNode.insertBefore(row, prevRow);
  }
};

对下限也有类似的限制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM