简体   繁体   English

如何使用jQuery引用div的定义值

[英]How can I reference a div's defined value using jquery

I am trying to reference a div with a predetermined value I made using jquery, and can't seem to find the appropriate method. 我正在尝试使用我使用jquery生成的具有预定值的div进行引用,但似乎找不到合适的方法。

I've tried using the .find() and .attr() methods to no avail. 我尝试使用.find()和.attr()方法无济于事。 I have a feeling this has to do with scope but I'm at a loss. 我觉得这与范围有关,但我很茫然。

I am trying to make changes to the neighboring divs (.cube) based on their row and col value. 我试图根据其行和col值对相邻的div(.cube)进行更改。 my alert script keeps returning an undefined result. 我的警报脚本不断返回未定义的结果。

  <div class="cube" id="1" row="1" col="1"></div> <div class="cube" id="2" row="1" col="2"></div> <div class="cube" id="3" row="1" col="3"></div> <div class="cube" id="4" row="1" col="4"></div> $(".cube").each(function() { var adjacentrowplus = +$(this).attr('row') + 1; var adjacentcolplus = +$(this).attr('col') + 1; var adjacentrowminus = +$(this).attr('row') - 1; var adjacentcolminus = +$(this).attr('col') - 1; if ($(".cube").attr("row") == adjacentrowplus) { console.log("the div equals the row"); } else { console.log("the div doesnt equal the row"); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="cube" id="1" row="1" col="1"></div> <div class="cube" id="2" row="1" col="2"></div> <div class="cube" id="3" row="1" col="3"></div> <div class="cube" id="4" row="1" col="4"></div> 

Your logic is just a bit off. 您的逻辑有点偏离。 If you look at your if statement as pseudo code we can see it will never return true : 如果将if语句视为伪代码,我们将看到它永远不会返回true

First iteration only: 仅第一次迭代:

if 1 == 1 + 1 then print "the div equals the row"

As you can see, adding one to the value of row , then checking that against the value of row doesn't make sense. 如您所见,在row的值上加上一个,然后对照row的值进行检查是没有意义的。

Instead, check the incremented row value against the id attribute. 相反,请对照id属性检查增加的row值。

In addition, you were checking against $(".cube").attr("row") which will always check only the first one. 另外,您正在检查$(".cube").attr("row") ,该命令将始终仅检查第一个。 If that makes sense, I'll change my answer to reflect that. 如果这有意义,我将更改答案以反映这一点。 But, I assumed you wanted to check the currently iterating .cube . 但是,我假设您想检查当前正在迭代的.cube

EDIT To accommodate the updated goal: identify the neighboring elements 编辑以适应更新的目标: 确定相邻元素

This becomes a much easier problem to solve then. 然后,这将变得更容易解决。 Given the structure provided in the question below you will find a jQuery version and straight JavaScript solution: 给定下面问题中提供的结构,您将找到一个jQuery版本和直接的JavaScript解决方案:

 let id = 2; function findNeighborsJQ(id) { console.log('With jQuery:'); console.log('Prev cube: ', $('#'+id).prev('div.cube').attr('id')); console.log('Next cube: ', $('#'+id).next('div.cube').attr('id')); } function findNeighbors(id) { console.log('Without jQuery:'); console.log('Prev cube: ', document.getElementById(id).previousElementSibling.id); console.log('Next cube: ', document.getElementById(id).nextElementSibling.id); } console.log('Given and id of "'+id+'"...'); findNeighborsJQ(id); findNeighbors(id); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="cube" id="1" row="1" col="1"></div> <div class="cube" id="2" row="1" col="2"></div> <div class="cube" id="3" row="1" col="3"></div> <div class="cube" id="4" row="1" col="4"></div> 

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

相关问题 如何使用Jquery通过它的属性值显示DIV - How can I show a DIV by it's attribute value using Jquery 使用jquery如果DIV的内容为空,我怎么能删除它? - Using jquery how can I remove a DIV if it's contents are empty? 如何使用jQuery将div的滚动条保持在div的底部? - How can I keep a div's scrollbar at the bottom of the div using jQuery? 我如何通过使用jQuery调整div的大小来调整div的字体大小? - how can i resize font of div by resizing the div using jQuery? 如何使用JQuery(或JavaScript)将计算出的百分比指定为DIV的保证金? - How can I assign a calculated percentage as a DIV's margin using JQuery (or JavaScript)? 我如何使用 jQuery 删除除特定 class 之外的所有 div 和其他 DOM HTML 元素 - How can i remove all div's and other DOM HTML elements except a particular class using jQuery 如何使用JQuery为具有特定类的所有div分配唯一ID - How can I assign a unique ID to all div's that have a specific class using JQuery 如何使用jQuery在同一个父元素内动态将“ left” px应用于div的宽度 - How can I dynamically apply the “left” px to a div's width, within the same parent element, using jQuery 使用jQuery引用表单元格的值 - Reference Table Cell's Value using jQuery 如何使用 JQuery 替换 div 的内容而不是附加值? - How can I replace the content of a div instead append a value using JQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM