我得到了这个 html 片段:
<label>
<input type="checkbox"/>
text here
</label>
在我的js中,我想检索“此处的文本”值
这是我的js:
$('input[type=checkbox]').click(function (e) {
var msg = e.target.closest???
});
最佳答案
您可以使用 DOM 节点 nextSibling
属性:
$('input[type=checkbox]').click(function (e) {
var msg = this.nextSibling.nodeValue;
console.log(msg);
});
$('input[type=checkbox]').click(function (e) {
var msg = this.nextSibling.nodeValue;
alert(msg);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label>
<input type="checkbox" />text here
</label>
关于javascript - 如何获取最接近html元素标签的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31220583/