如何查找父级最近的锚点标记
How to find parent closest anchor tag
我正在尝试更改锚点标记的背景色。为了找到这个锚标记,我有一个带有id的跨度。那么我该如何找到这个锚标签呢。Html代码低于
<li class="fa-2x lesson_strand active">
<a onclick="window.open('/wwtb/api/viewer.pl?uid=demo_user&cid=1&wid=NAGM15GRA4_HWK4OAC1&role=Student','_blank','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=1014,height:655,left=150,top=50,titlebar=yes')" href="#">
<b class="fa_text">Domain 4.OA Cluster 1 Quiz (Homework)</b>
</a>
<a onclick="window.open('/ePC/ePlannerAssessment.do?isbn=9780544349179&isSoar=0&toolType=12&uniqueID=NAGM15GRA4_HWK4OAC1&resourceBUID=NAGM15GRA4_HWK4OAC1&nextGenTool=WB%20HTTP/1.1','_blank','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=1014,height:655,left=150,top=50,titlebar=yes')"
href="#">
<span id="assignButton_NAGM15GRA4_HWK4OAC1" class="assignButton" onclick="assignclicksToButton(event)">Assign</span>
</a>
</li>
我正在尝试更改第一个锚标记的颜色,而我只有跨度id。我该怎么做呢。请帮忙。
谢谢!
使用.closest()
(jQuery文档链接)
$(this).closest('a');
来自API文档:
对于集合中的每个元素,获取与选择器,方法是测试元素本身并向上遍历其DOM树中的祖先。
先使用.parent()
,然后使用.prev
方法$("#assignButton_NAGM15GRA4_HWK4OAC1").parent().prev().css({"color": "red", "border": "2px solid red"});
$("#assignButton_NAGM15GRA4_HWK4OAC1").parent().prev().css({"color": "red", "border": "2px solid red"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="fa-2x lesson_strand active">
<a onclick="window.open('/wwtb/api/viewer.pl?uid=demo_user&cid=1&wid=NAGM15GRA4_HWK4OAC1&role=Student','_blank','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=1014,height:655,left=150,top=50,titlebar=yes')" href="#">
<b class="fa_text">Domain 4.OA Cluster 1 Quiz (Homework)</b>
</a>
<a onclick="window.open('/ePC/ePlannerAssessment.do?isbn=9780544349179&isSoar=0&toolType=12&uniqueID=NAGM15GRA4_HWK4OAC1&resourceBUID=NAGM15GRA4_HWK4OAC1&nextGenTool=WB%20HTTP/1.1','_blank','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=1014,height:655,left=150,top=50,titlebar=yes')"
href="#">
<span id="assignButton_NAGM15GRA4_HWK4OAC1" class="assignButton" onclick="assignclicksToButton(event)">Assign</span>
</a>
</li>
取closest
li
,然后取find
,取该li
的first
a
。希望下面给出的代码片段能对你有所帮助。
$('#assignButton_NAGM15GRA4_HWK4OAC1').closest('li')
.find('a:first').css('background-color', 'green');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="fa-2x lesson_strand active">
<a onclick="window.open('/wwtb/api/viewer.pl?uid=demo_user&cid=1&wid=NAGM15GRA4_HWK4OAC1&role=Student','_blank','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=1014,height:655,left=150,top=50,titlebar=yes')" href="#">
<b class="fa_text">Domain 4.OA Cluster 1 Quiz (Homework)</b>
</a>
<a onclick="window.open('/ePC/ePlannerAssessment.do?isbn=9780544349179&isSoar=0&toolType=12&uniqueID=NAGM15GRA4_HWK4OAC1&resourceBUID=NAGM15GRA4_HWK4OAC1&nextGenTool=WB%20HTTP/1.1','_blank','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=1014,height:655,left=150,top=50,titlebar=yes')"
href="#">
<span id="assignButton_NAGM15GRA4_HWK4OAC1" class="assignButton" onclick="assignclicksToButton(event)">Assign</span>
</a>
</li>
请尝试以下操作:
$("#assignButton_NAGM15GRA4_HWK4OAC1").closest('a').css({"background-color": "red"});
相关文章:
- 在谷歌地图上查找最近的大头针
- 如何查找离单击位置最近的子项
- 使用JavaScript查找数组中最近的日期
- jQuery查找最近的按钮并触发点击
- 算法导航:如何查找最近的日期(javascript)
- 查找所选元素的最近链接
- 如何查找最近的元素
- 在没有 jQuery 的情况下查找最近的元素
- 使用多个选择器查找DOM中最近的元素
- 使用jQuery查找离滚动位置最近的HTML片段标识符
- javascript数组查找具有当前日期+日期的最近日期
- 使用javascript查找元素相对于最近相对定位的父元素的位置
- 如何查找父级最近的锚点标记
- 查找离给定经度/纬度最近的城市
- 在元素中查找最近的td
- 使用jQuery查找父元素和最近的元素
- jQuery查找最近的ul
- 在数组中查找离我最近的数字
- 在点击的标签上查找最近的图像
- 查找最近的最外层导航(多个嵌套导航元素)