如何获取最接近html元素标记的文本

kim*_*doe 0 javascript jquery

我有这个html片段:

<label>
  <input type="checkbox"/>
  text here
</label>
Run Code Online (Sandbox Code Playgroud)

在我的js中,我想检索'text here'值

这是我的js:

$('input[type=checkbox]').click(function (e) {
var msg = e.target.closest???
});
Run Code Online (Sandbox Code Playgroud)

A. *_*lff 5

您可以使用DOM节点nextSibling属性:

$('input[type=checkbox]').click(function (e) {
    var msg = this.nextSibling.nodeValue;
    console.log(msg);
});
Run Code Online (Sandbox Code Playgroud)

$('input[type=checkbox]').click(function (e) {
    var msg = this.nextSibling.nodeValue;
    alert(msg);    
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label>
    <input type="checkbox" />text here
</label>
Run Code Online (Sandbox Code Playgroud)