如何在不返回<span>的值的情况下返回类<div>的值

时间:2016-08-19 15:39:11

标签: javascript jquery html

如何在没有“x”的情况下返回“Jhon Doe”的值

https://jsfiddle.net/sok1hkkt/3/

<div class="chip"><span class="closebtn" onclick="this.parentElement.remove()">&times;</span>John Doe</div>
<script>
var array = $( ".chip" )
  .map(function() {
    return this.textContent; //changeing only this line
  })
  .get()
  .join(); 
  alert(array)
</script>

3 个答案:

答案 0 :(得分:1)

尝试此代码

var getText =$('.chip').clone()
            .children()
            .remove()
            .end()
            .text();

      alert(getText);

答案 1 :(得分:1)

我们可以这样做:

https://jsfiddle.net/sok1hkkt/6/

{{1}}

答案 2 :(得分:1)

你可以这样做:

// Take the whole inner HTML, split it at the span closing tag,
// take the second element which is the desired text
var text= $( ".chip" ).html().split('</span>')[1];
alert(text);

https://jsfiddle.net/sok1hkkt/4/

如果您需要多个元素,可以使用jQuery .each() function,如下所示:

var array = [];

$( ".chip" ).each(function(){
    array.push($(this).html().split('</span>')[1]);
});

alert(array);

https://jsfiddle.net/sok1hkkt/7/