Javascript - 在单击另一个子元素后获取子元素的值

时间:2017-04-27 16:37:16

标签: javascript html dom

这一定非常简单,但我仍然无法理解......下面是一个例子和我尝试的内容 - 我想要的是在点击{{1}后得到<a id="{value}"..的值DIV。

&#13;
&#13;
<div class="text"
&#13;
.h {
  background: lightblue;
}

.pb {
  display: inline-block
}

.cb {
  clear: both
}

.text {
  background: #ffc;
  border: 1px solid #000
}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

由于元素具有ID,您只需通过该ID查找即可。 ID必须是唯一的(但请继续阅读,我怀疑你不想这样做)

alert(document.getElementById('m75813').id);

.h {
  background: lightblue;
}

.pb {
  display: inline-block
}

.cb {
  clear: both
}

.text {
  background: #ffc;
  border: 1px solid #000
}
<article>
  <div class="h">
    <div class="pb">user</div> &nbsp;
    <div class="pb">date <a id="m75813" href="#m75183">#</a></div>
  </div>

  <div class="text" onclick="alert(document.getElementById('m75813').id);">
    After clicking on the yellow background, the value from [a id="m75813"] (ie. 'm75813') should apppear. Sentence text.<br> Other sample.<br> Hello world.
  </div>

</article>

如果你有很多这些带有各种ID并且它是你想要找到的ID,你可以通过上升到父节点并使用querySelector来实现找到a元素:

alert(this.parentNode.querySelector('a').id);

.h {
  background: lightblue;
}

.pb {
  display: inline-block
}

.cb {
  clear: both
}

.text {
  background: #ffc;
  border: 1px solid #000
}
<article>
  <div class="h">
    <div class="pb">user</div> &nbsp;
    <div class="pb">date <a id="m75813" href="#m75183">#</a></div>
  </div>

  <div class="text" onclick="alert(this.parentNode.querySelector('a').id);">
    After clicking on the yellow background, the value from [a id="m75813"] (ie. 'm75813') should apppear. Sentence text.<br> Other sample.<br> Hello world.
  </div>

</article>
<article>
  <div class="h">
    <div class="pb">user</div> &nbsp;
    <div class="pb">date <a id="m54684" href="#m54684">#</a></div>
  </div>

  <div class="text" onclick="alert(this.parentNode.querySelector('a').id);">
    After clicking on the yellow background, the value from [a id="m54684"] (ie. 'm54684') should apppear. Sentence text.<br> Other sample.<br> Hello world.
  </div>

</article>

在这两种情况下,我都建议使用现代事件处理,而不是使用onxyz - 属性样式处理程序。例如:假设所有这些article元素都在某种容器中。我们可以在容器上挂钩click,然后找到点击过的div,找到与a相关的div

document.getElementById("container").addEventListener("click", function(e) {
  var element = e.target;
  while (element != this) {
    if (element.matches("div.text")) {
      alert(element.parentNode.querySelector("a").id);
      break;
    }
    element = element.parentNode;
  }
});

document.getElementById("container").addEventListener("click", function(e) {
  var element = e.target;
  while (element != this) {
    if (element.matches("div.text")) {
      alert(element.parentNode.querySelector("a").id);
      break;
    }
    element = element.parentNode;
  }
});
.h {
  background: lightblue;
}

.pb {
  display: inline-block
}

.cb {
  clear: both
}

.text {
  background: #ffc;
  border: 1px solid #000
}
<div id="container">
  <article>
    <div class="h">
      <div class="pb">user</div> &nbsp;
      <div class="pb">date <a id="m75813" href="#m75183">#</a></div>
    </div>

    <div class="text">
      After clicking on the yellow background, the value from [a id="m75813"] (ie. 'm75813') should apppear. Sentence text.<br> Other sample.<br> Hello world.
    </div>

  </article>
  <article>
    <div class="h">
      <div class="pb">user</div> &nbsp;
      <div class="pb">date <a id="m54684" href="#m54684">#</a></div>
    </div>

    <div class="text">
      After clicking on the yellow background, the value from [a id="54684"] (ie. '54684') should apppear. Sentence text.<br> Other sample.<br> Hello world.
    </div>

  </article>
</div>

答案 1 :(得分:0)

你可以这样做:

<div class="text" onclick="alert(document.getElementById('m75813').id);">
    After clicking on the yellow background, the value from [a id="m75813"] (ie. 'm75813') should apppear. Sentence text.<br> Other sample.<br> Hello world.
  </div>

答案 2 :(得分:0)

发生的事情是您的查询返回文本节点(包括像换行符这样的空格)以及HTML节点,因此您的元素不在您期望的索引处。 querySelector('a')是更简单的方法,但是如果您 希望通过它在DOM结构中的位置选择目标,则可以使用firstChildElementchildren代替分别为firstChildchildNodes,因为它们排除了文本节点:

this.parentElement.firstElementChild.children[1].firstElementChild.id

.h {
  background: lightblue;
}

.pb {
  display: inline-block
}

.cb {
  clear: both
}

.text {
  background: #ffc;
  border: 1px solid #000
}
<article>
  <div class="h">
    <div class="pb">user</div> &nbsp;
    <div class="pb">date <a id="m75813" href="#m75183">#</a></div>
  </div>

  <div class="text" onclick="alert(this.parentElement.firstElementChild.children[1].firstElementChild.id);">
    After clicking on the yellow background, the value from [a id="m75813"] (ie. 'm75813') should apppear. Sentence text.<br> Other sample.<br> Hello world.
  </div>

</article>