简体   繁体   English

如何使用javascript中的向上和向下箭头键在列表中移动所选项目?

[英]how to move selected item in a list using up and down arrow keys in javascript?

I am trying to implement up and down arrow buttons for a list in HTML. 我正在尝试为HTML列表实现上下箭头按钮。 the up arrow moves the selected element of list upwards and the down arrow moves the selected list element downwards. 向上箭头将所选列表元素向上移动,而向下箭头则将所选列表元素向下移动。 I tried this code, but not working :: 我尝试了这段代码,但无法正常工作::

 function reorder_up(node) { $(".go_up").click(function() { var $current = $(this).closest('li') var $previous = $current.prev('li'); if ($previous.length !== 0) { $current.insertBefore($previous); } return false; }); } function reorder_down(node) { $(".go_down").click(function() { var $current = $(this).closest('li') var $next = $current.next('li'); if ($next.length !== 0) { $current.insertAfter($next); } return false; }); } // for adding to the result page i am using this function, where i am creating a list dynamically and provinding the id to the selected element when clicking on it. I need to move up - down in the result section of the list :: function add_to_result() { //var moparent = document.getElementById("parent").innerHTML; var moname = document.getElementById("moselect").innerHTML; var node = document.createElement('LI'); node.setAttribute('onclick', 'giveid_Result(this)'); node.setAttribute('ondblclick', 'fillprops()'); var text = document.createTextNode(moname); node.appendChild(text); document.getElementById("result").appendChild(node); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button type="button" class='go_up' onclick="reorder_up(this)" style="height:40px;width:40px">&uarr;</button> <button type="button" class='go_down' onclick="reorder_down(this)" style="height:40px;width:40px">&uarr;</button> <div id="results"> <div class="boxheader"> <STRONG>RESULTS</STRONG> </div> <div class="boxcontent"> <ul id="result"> </ul> </div> </div> 

 var selected; for(var x=0;x<5;x++){ addToResult("Node:"+x); } $("li").click(function(){ console.log("pressing"+$(this).attr("id")); select($(this)) }); $(".go_up").click(function(){reorder_up(selected)}); $(".go_down").click(function(){reorder_down(selected)}); function reorder_up(node) { var dnode=node; console.log("RUP"); var $current = $(dnode).closest('li') $current.css("background-color","blue"); var $previous = $current.prev('li'); $previous.css("background-color","yellow"); if ($previous.length !== 0) { $current.insertBefore($previous); } return false; } function reorder_down(node) { console.log("RDO"); var dnode=node; var $current = $(dnode).closest('li') $current.css("background-color","blue"); var $next = $current.next('li'); $next.css("background-color","yellow"); if ($next.length !== 0) { $current.insertAfter($next); } return false; } // for adding to the result page i am using this function, where i //am creating a list dynamically and provinding the id to the selected //element when clicking on it. I need to move up - down in the result section of the list // :: function addToResult(id) { var node = document.createElement('li'); node.setAttribute('id',id); // node.setAttribute('onclick', 'select(id)'); node.setAttribute('ondblclick', 'fillprops()'); var text = document.createTextNode(id); node.appendChild(text); document.getElementById("result").appendChild(node); } function select(selector){ selector.css("background-color","green"); console.log("youre pressing"+selector.attr("id")); selected=selector; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type="button" class='go_up' style="height:40px;width:40px">&uarr;</button> <button type="button" class='go_down' style="height:40px;width:40px">&darr;</button> <div id="results"> <div class="boxheader"> <STRONG>RESULTS</STRONG> </div> <div class="boxcontent"> <ul id="result"> </ul> </div> </div> 

The $(something).click query executes when you click on that something. $(something).click查询在单击某事物时执行。 So it doesn't make sense to put into a function. 因此,放入一个函数没有任何意义。 You want it to trigger some function execution. 您希望它触发一些函数执行。 I added the dnode var to keep the scope of the node since this is no longer attached to the node when you call reorder node. 我添加了dnode var来保留节点的范围,因为在您调用reorder节点时,该节点不再附加到该节点。 So I substituted this with dnode and it works fine. 所以我用dnode代替了 ,并且工作正常。 Here is the fully working code : 这是完整的工作代码

Javascript: 使用Javascript:

for(var x=0;x<5;x++){
    add_to_result("Node"+x);
}
$("li").click(function(){      
   select($(this))
 });

$(".go_up").click(function() {
 reorder_up(selectedNode);
  });

  $(".go_down").click(function() {
      reorder_up(selectedNode);
  });

    function reorder_up(node) {
    var dnode=node;
    var $current = $(dnode).closest('li')
    var $previous = $current.prev('li');
    if ($previous.length !== 0) {
      $current.insertBefore($previous);
    }
    return false;

}


function reorder_down(node) {
    var dnode=node;
    var $current = $(node).closest('li')
    var $next = $current.next('li');
    if ($next.length !== 0) {
      $current.insertAfter($next);
    }
    return false;
}

You can remove the onclik attribute from html. 您可以从html删除onclik属性。 It is not needed 不需要

About the add to result function: There is no element with the moselect id, so moname is null and the compiler gives an error. 关于添加到结果函数:没有带有moselect id的元素,因此moname为null,编译器给出了错误。 Next, you never call it so it will never execute. 接下来,您永远不会调用它,因此它将永远不会执行。 You should add a loop somewhere to add the elements. 您应该在某处添加一个循环以添加元素。 You don't need to set attribute onclick , just use a jquery selector.I added set attribute id to pass it to select function. 您不需要设置属性onclick ,只需使用一个jquery选择器即可。我添加了set属性id来将其传递给select函数。 By the way, I don't see give_id function anywhere so I created a function to select the node to be moved 顺便说一下,我在任何地方都看不到Give_id函数,所以我创建了一个函数来选择要移动的节点

function add_to_result(id) {  
  var node = document.createElement('li');
  node.setAttribute('id',id);
  node.setAttribute('ondblclick', 'fillprops()');
  var text = document.createTextNode(id);
  node.appendChild(text);
  document.getElementById("result").appendChild(node);
}
function select(selector){
  selector.css("background-color","green");
  console.log("youre pressing"+selector.attr("id"));
  selected=selector;
}

Your jQuery click event listeners are added after the click event happens so they are never handled, on the click event you are just binding the click handler. 您的jQuery click事件监听器是在click事件发生后添加的,因此它们永远不会被处理,在click事件上,您只是绑定了click处理程序。 Move the $().click outside of the onclick and since you are using jQuery selectors for the buttons you can get rid on onclick $().click onclick之外,由于您使用jQuery选择器作为按钮,因此可以摆脱onclick

 $(".go_up").click(function() { var $current = $(this).closest('li') var $previous = $current.prev('li'); if ($previous.length !== 0) { $current.insertBefore($previous); } return false; }); $(".go_down").click(function() { var $current = $(this).closest('li') var $next = $current.next('li'); if ($next.length !== 0) { $current.insertAfter($next); } return false; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button type="button" class='go_up' style="height:40px;width:40px">&uarr;</button> <button type="button" class='go_down' style="height:40px;width:40px">&uarr;</button> <div id="results"> <div class="boxheader"> <STRONG>RESULTS</STRONG> </div> <div class="boxcontent"> <ul id="result"> </ul> </div> </div> 

暂无
暂无

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

相关问题 如何向右移动光标焦点。 在 JavaScript 中使用箭头键向左、向上和向下 - How to move cursor focus right. left , up and down using arrow keys in JavaScript 如何使用箭头键在网格中的行之间向上/向下移动图像? (JavaScript) - How do I move an image Up/Down between rows in a grid using arrow keys? (JavaScript) 如何使用箭头键在ASP.NET Gridview中上下移动 - How to Move up and down in ASP.NET Gridview Using Arrow Keys Svelte:按向上箭头和向下箭头键时,如何将焦点设置到列表项中的上一个/下一个元素? - Svelte: How can I set the focus to the previous/next element in the list item when pressing UP arrow and DOWN arrow keys? 使用javascript使用上下箭头键访问Div目录 - Access Div Contents using Up and Down Arrow Keys using javascript 使用Javascript(jQuery)使用箭头键(上下)选择li元素 - Select li element with arrow keys (up and down) using Javascript (jQuery) 使用上下箭头键在UL LI列表中滚动 - Using the up and down arrow keys to scroll through a UL LI list 在 javascript 中使用箭头键移动选定元素 - move selected element with arrow keys in javascript 如何使用自定义搜索字段上的向下箭头键将焦点移至列表项? - How to move focus to a list item on using the down arrow key on a custom search field? 如何在反应中使用向上和向下箭头键使下拉菜单工作 - how to make dropdown work using up and down arrow keys in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM