[英]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.