如何在没有“x”的情况下返回“Jhon Doe”的值
https://jsfiddle.net/sok1hkkt/3/
<div class="chip"><span class="closebtn" onclick="this.parentElement.remove()">×</span>John Doe</div>
<script>
var array = $( ".chip" )
.map(function() {
return this.textContent; //changeing only this line
})
.get()
.join();
alert(array)
</script>
答案 0 :(得分:1)
尝试此代码
var getText =$('.chip').clone()
.children()
.remove()
.end()
.text();
alert(getText);
答案 1 :(得分: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);