gpt4 book ai didi

javascript - 如何在 Javascript 中向上或向下移动文本行

转载 作者:行者123 更新时间:2023-11-30 21:06:19 24 4
gpt4 key购买 nike

我遇到了关于向上或向下移动文本行的问题,当我向上或向下移动一行时,它可以移动但似乎旧行仍然存在并且没有被删除 enter image description here

例如:我向下移动“文本 2”,它可以移动但行仍然存在

这是我的来源:

<head>
<script>
function down_move(index)
{
var frm = document.writeForm;
var opts=frm["ans_list" + index].options

for (var i=opts.length-1; i>=0; i--) {
if (opts[i].selected && i<opts.length-1) {
tmp = opts[i].cloneNode(true);
// opts[i].removeChild(true);
opts[i].removeChild(opts[i].childNodes[0]);
opts[i].insertAdjacentElement("afterEnd", tmp).selected = true;
}
}
setting_val(index);
}
</script>
</head>

<body>
<div>
<a href="#" onClick="javasript:down_move('<%=i+1%>');" style="float:left"><span class="bt_test_admin bg_type_01">▼ Order</span></a>
</div>
</body>

我从控制台错误浏览器收到一条消息:

Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'.

如何修复错误?谢谢

最佳答案

你必须找到按钮的父 tr ,然后找到之前附加到的元素。因为没有在一个元素之后追加的功能,所以当你想向下移动时,你必须找到第二个下一个元素。

只需将这两个函数分别应用于上行和下行。我相信它对你有用

上移函数

function MoveUpRow() {
var table,
row = this.parentNode;

// find the parent tr ( the row element )
while ( row != null ) {
if ( row.nodeName == 'TR' ) {
break;
}
row = row.parentNode;
}
// the parent of the row is the table body
table = row.parentNode;
// insert the row before it's previevs siblings
table.insertBefore ( row, get_previoussibling( row ) );
}

下移行功能

function MoveDownRow() {
var table,
row = this.parentNode;

while ( row != null ) {
if ( row.nodeName == 'TR' ) {
break;
}
row = row.parentNode;
}
table = row.parentNode;
// you have to find the second next siblings to append before
// NOTE: if the second parameter of the 'insertBefore' function is null
// it will append the row to the table!
table.insertBefore ( row, get_nextsibling ( get_nextsibling( row ) ) );
}

关于javascript - 如何在 Javascript 中向上或向下移动文本行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46556064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com