LinkedList findElement(LinkedList findElement)
我尝试使用下面的代码来查找
LinkedList
最后一个第k个元素。 为什么它总是返回null
?public Node findElem(Node head, int k){ if(k < 1|| k > this.length()){ System.out.println("error"); } Node p1=head; Node p2 = head; for(int i=0;i<k-1;i++) { p1 = p1.next; } while(p1 != null){ p1= p1.next; p2 = p2.next; } return p2; }
I try to use below code to find the last k-th element in the
LinkedList
. Why does it always returnnull
?public Node findElem(Node head, int k){ if(k < 1|| k > this.length()){ System.out.println("error"); } Node p1=head; Node p2 = head; for(int i=0;i<k-1;i++) { p1 = p1.next; } while(p1 != null){ p1= p1.next; p2 = p2.next; } return p2; }
原文:https://stackoverflow.com/questions/45652154
更新时间:2023-08-27 16:08
最满意答案
这是解决方案的一部分。 您应该能够弄清楚如何实现其余的:
var items = document.getElementsByClassName("item"); Array.prototype.forEach.call(items, function(element) { element.addEventListener("mouseover", function() { elements = this.querySelectorAll(".qLeft>img"); Array.prototype.forEach.call(elements, function(el) { el.style.boxShadow = "2px 2px 5px -1px black"; }); // same for qRight }); element.addEventListener("mouseout", function() { // ... }); });
Here is part of the solution. You should be able to figure out how to implement the rest:
var items = document.getElementsByClassName("item"); Array.prototype.forEach.call(items, function(element) { element.addEventListener("mouseover", function() { elements = this.querySelectorAll(".qLeft>img"); Array.prototype.forEach.call(elements, function(el) { el.style.boxShadow = "2px 2px 5px -1px black"; }); // same for qRight }); element.addEventListener("mouseout", function() { // ... }); });
相关问答
更多-
没关系,因为如果必须的话,你总是可以将任何DOM元素包装在jQuery对象周围。 var test = document.getElementById('test-table'); // Do some vanilla stuff var jTest = $(test); // Do some jQuery jQuery库建立在JavaScript可用的DOM API之上。 你需要jQuery的唯一原因是做一个复杂的任务,需要花费更多的努力。 在性能方面,差异可以忽略不计。 jQuery添加了支持跨浏览器 ...
-
您需要使用不同的属性才能访问文档和窗口高度。 document.clientHeight应该是document.body.clientHeight 。 clientHeight属性旨在返回HTML元素的计算高度。 使用body元素适合于该设计。 window.clientHeight应该是window.innerHeight 。 由于window不是HTML元素,因此它有自己的高度属性 。 我还简化了进度条属性设置逻辑。 除非您有一些外部要求来设置data-max和data-value属性,否则可以删除这些 ...
-
这是解决方案的一部分。 您应该能够弄清楚如何实现其余的: var items = document.getElementsByClassName("item"); Array.prototype.forEach.call(items, function(element) { element.addEventListener("mouseover", function() { elements = this.querySelectorAll(".qLeft>img"); Array. ...
-
首先请注意,您的jQuery示例比它需要的更复杂。 你可以这样做: $('.bar-toggle').on('click', function() { $('#container').toggleClass("with_toggle", localStorage.toggled != "with_toggle"); localStorage.toggled = localStorage.toggled == "with_toggle" ? '' : 'with_toggle'; }); 要将其转换 ...
-
简单的答案是,这是不可能的,因为jQuery提供了一个基于香草JS的事件层。 这意味着,香草JS无法与添加的图层进行交谈。 总而言之,jQuery可以捕获vanilla JS事件,但是vanilla JS无法捕获jQuery添加的事件。 The short answer is that this is impossible as jQuery provides an event layer over vanilla JS. That means that vanilla JS cannot talk to ...
-
将jQuery转换为vanilla JS - 在h1之后插入p元素(Turn jQuery into vanilla JS - Insert p element after h1)[2023-09-07]
jQuery在幕后为你做了很多。 等效的纯DOM代码可能如下所示: // Get all header elements var header = document.getElementsByTagName('h1'), parent, newP, text; // Loop through the elements for (var i=0, m = header.length; i < m; i++) { parent = header[i].parentNode; ... -
查询文档以使用所需的选择器,在这种情况下: .analytics:not(#promo) 将NodeList转换为数组 使用谓词过滤数组: element => element.querySelector('your-selector') 如果没有找到子元素, element.querySelector('your-selector')将计算为null (这是假的) 一般作为一种功能 function has(nodeList, selector) { return Array.from(nodeLi ...
-
我发现编写这两个函数在从jQuery转换到本地JS时确实有帮助。 function domEach(selector, handler, context) { return Array.from(document.querySelectorAll(selector), handler, context); } // If you get a TypeError "Array.from" is not a function, use the polyfill // found on MPN. // ...
-
一种选择是使用CSS选择器以及.querySelectorAll()方法 。 由于CSS具有:not()伪类 ,因此可以使用它来取消script , style和iframe元素。 var elements = document.querySelectorAll('*:not(script):not(style):not(iframe'); 在上面的代码片段中, elements将是包含所选元素集合的NodeList 。 One option would be to use a CSS selector ...
-
您没有告诉服务器您如何编码请求中的数据。 r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 据推测,您用于处理数据的任何服务器端处理程序都没有正确解析它,因此找不到所需的数据,然后返回一个空白数组作为结果。 You haven't told the server how you are encoding the data in the request. r.setRequestHeader("Content-T ...