3

给定下一个 HTML 块:

<div class"radioGroup">
   <input type="radio" value="0">
        This is NOT checked
   <input type="radio" value="1" checked = "checked" > 
        This is checked
   <input type="radio" value="2">
        This is NOT checked
</div>

如何选择所选单选按钮后面的文本?(例如“已检查”)。

(我不能在字符串中添加标签或类似的东西,我正在尝试从另一个网页中获取此文本,所以我只能使用客户端脚本)

4

1 回答 1

1

您可以使用:

<input ... onclick="alert(this.nextSibling.data);">

当然,这非常依赖于 DOM 的结构。您可能希望将所有文本节点中的数据从该元素连接到下一个元素,因此您可能需要如下函数:

function getTextByNodes(el) {
  var text ='';
  while (el && el.nextSibling && el.nextSibling.nodeType == 3) {
    text += el.nextSibling.data;
    el = el.nextSibling;
  }
  return text;
}

和:

<input ... onclick="alert(getTextByNodes(this));">
于 2013-06-12T23:56:17.177 回答