首页 > 解决方案 > 如何获取兄弟元素的内部html文本

问题描述

为什么这不打印出跨度的文本“Apple”?

<label class="search-recipe-chkbox fruitLabel">
  <input type="checkbox" class="checkbox check-box-mob-filters check-box-desktop" id="12DSKT" name="Apple" value="12">
  <span class="checkmark"></span>
  <span class="ingredient-label">Apple <span class="recipe-count" id="12numDSKT">(1)</span></span>
</label>
for (var checkbox of checkboxes) {
  if (checkbox.checked == true) {
    console.log($(checkbox.id).siblings('.ingredient-label').text());
  }
}

checkboxes只是表单中的所有复选框(输入),并且已选中 Apple。

谢谢

标签: javascripthtmljquery

解决方案


您也可以使用 -console.log($("[id]").siblings('.ingredient-label').text());


推荐阅读