如何使用Javascript获取包含另一个跨度的跨度的值?

时间:2016-05-24 11:38:08

标签: javascript html

我在另一个标签中有一个public static int f(int a, int b, int c) { int result = 0 ; if ((a < b) && (b < c)) result = a; else if ((a >= b) && (b >= c)) result = b; else if ((a == b) || (b == c) || (a == c)) result = c; return result; } 标签。

<span>

我希望只获取第一个<span id="count">255<span>words</span></span> 代码的值,即值<span>

我尝试过:

255

我知道我能做到:

var words = document.getElementById('count'); //Gets the <span>

alert(words.value); //undefined --> does not exist for <span> tag
alert(words.innerHTML); //255<span>words</span>
alert(words.innerText); //255words
alert(words.textContent); //255words

但我想直接这样做。

是否可以只检索第一个跨度的值?

5 个答案:

答案 0 :(得分:3)

首先获取SPAN元素,然后选择它的第一个子节点(即文本节点),然后获取它的值

document.getElementById('count').firstChild.nodeValue

注意:这将返回文本,如果你想要int整数,那么你需要使用带有十进制基数的parseInt函数解析它(基数为10)。

parseInt(document.getElementById('count').firstChild.nodeValue, 10)

答案 1 :(得分:1)

种类变种

&#13;
&#13;
var allNodes = document.getElementById('count').childNodes;
var text="";
for( var i in allNodes ){
   if( allNodes[i].nodeType == 3 ){
     text += " "+allNodes[i].wholeText;
   }
}

console.log( text );
&#13;
<span id="count">255<span>words</span><div>text</div>text</span>
&#13;
&#13;
&#13;

第一级文字的

已更新代码段,即使还有其他元素

答案 2 :(得分:0)

这是一种获取未包含在子元素中的所有文本的方法:

function getText(node) {
    // Take clone of the node
    var node = node.cloneNode(true);
    // remove child elements from it, not text nodes
    for (child of node.childNodes){
        if (child.nodeType !== 3) node.removeChild(child);
    }
    return node.textContent;
}

console.log (getText(document.querySelector('span#count')));
<span id="count">255 <span>words</span> or 127</span>

答案 3 :(得分:0)

虽然您已经接受了答案,但我还想提供其他几个选项:

function getChildTextOf (n) {
  // retrieving the childNodes of the passed-in node (n)
  // and converting those nodes into an Array
  let text = Array.from( n.childNodes )
    // filtering that Array to retain only those nodes
    // whose nodeType is equal to 3 (and so are textNodes)
    .filter( n => n.nodeType === 3 )
    // returning a new Array composed of the nodeValue
    // (the text) of those nodes:
    .map(n => n.nodeValue);

  // if we have a truthy value stored in the text variable,
  // and that that text variable (an Array) is of length
  // 1 we return the first element of the Array (a String),
  // otherwise we return the whole Array:
  return text && text.length === 1 ? text.shift() : text;
}

console.log(getChildTextOf(document.getElementById('count')));

function getChildTextOf(n) {
  let text = Array.from(n.childNodes).filter(n => n.nodeType === 3).map(n => n.nodeValue);

  return text && text.length === 1 ? text.shift() : text;
}

console.log(getChildTextOf(document.getElementById('count')));
<span id="count">255<span>words</span></span>

JS Fiddle demo

function getChildTextOf (n) {

  // cloning the passed-in node (n), along
  // with its own children:
  let clone = n.cloneNode(true);

  // retrieving the children (child-elements) of the
  // cloned-node, converting that collection into an
  // Array, to use Array methods:
  Array.from( clone.children )

    // iterating over that Array of element-children
    // removing each child from its parent (the clone)
    .forEach(child => clone.removeChild(child));

  // normalising the clone, which combines all text-nodes
  // into one, removing any/all empty nodes:
  clone.normalize();

  // returning the textContent of that clone:
  return clone.textContent;
}

console.log(getChildTextOf(document.getElementById('count')));

function getChildTextOf(n) {
  let clone = n.cloneNode(true);
  Array.from(clone.children).forEach(child => clone.removeChild(child));
  clone.normalize();
  return clone.textContent;
}

console.log(getChildTextOf(document.getElementById('count')));
<span id="count">255<span>words</span></span>

JS Fiddle demo

参考文献:

答案 4 :(得分:-1)

你也可以使用它 $( '#计数')。内容()[0]