admin管理员组文章数量:1425787
how to get all the text
after <br/>
tag
my html is like this
<i class="loc">cherry<br>From Shimla</i>
<i class="loc">Apple<br>from kashmir</i>
<i class="loc">banana<br>From bihar</i>
expected output: ["From Shimla","from kashmir","From bihar"];
i'm trying something like this
var arr = [];
$('.loc').each(function(){
arr.push($(this).text());
});
console.log(arr);
<script src=".0.3/jquery.min.js"></script>
<i class="loc">cherry<br>From Shimla</i>
<i class="loc">Apple<br>from kashmir</i>
<i class="loc">banana<br>From bihar</i>
how to get all the text
after <br/>
tag
my html is like this
<i class="loc">cherry<br>From Shimla</i>
<i class="loc">Apple<br>from kashmir</i>
<i class="loc">banana<br>From bihar</i>
expected output: ["From Shimla","from kashmir","From bihar"];
i'm trying something like this
var arr = [];
$('.loc').each(function(){
arr.push($(this).text());
});
console.log(arr);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<i class="loc">cherry<br>From Shimla</i>
<i class="loc">Apple<br>from kashmir</i>
<i class="loc">banana<br>From bihar</i>
Share
Improve this question
edited Nov 29, 2016 at 11:05
Ben Fortune
32.2k10 gold badges81 silver badges81 bronze badges
asked Nov 29, 2016 at 11:02
Dilip GDilip G
5292 gold badges7 silver badges17 bronze badges
3 Answers
Reset to default 3You can target the br
element and use .get(index)
to fetch the underlying DOM element, the use nextSibling
to target the text node. Then nodeValue
property can be used to get the text.
var arr = [];
$('.loc').each(function() {
arr.push($(this).find('br').get(0).nextSibling.nodeValue);
});
console.log(arr);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<i class="loc">cherry<br>From Shimla</i>
<i class="loc">Apple<br>from kashmir</i>
<i class="loc">banana<br>From bihar</i>
You can further improve you code as
var arr = $('.loc').map(function() {
return $(this).find('br').get(0).nextSibling.nodeValue;
}).get();
console.log(arr);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<i class="loc">cherry<br>From Shimla</i>
<i class="loc">Apple<br>from kashmir</i>
<i class="loc">banana<br>From bihar</i>
var arr = [];
$('.loc').each(function(){
arr.push($(this).children('br').get(0).nextSibling);
});
console.log(arr);
var arr = [];
$('.loc').each(function(){
arr.push($(this).html().split('<br>')[1]);
});
console.log(arr);
You can use html(), split it, and use second value from array.
var arr = [];
$('.loc').each(function(){
arr.push($(this).html().split('<br>')[1]);
});
console.log(arr);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<i class="loc">cherry<br>From Shimla</i>
<i class="loc">Apple<br>from kashmir</i>
<i class="loc">banana<br>From bihar</i>
本文标签: javascripthow to get the text after ltbrgt tagStack Overflow
版权声明:本文标题:javascript - how to get the text after <br> tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745451984a2658928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论