jQuery:获取两个< br>之间的文本元素 [英] jquery: get text between two <br> elements

查看:210
本文介绍了jQuery:获取两个< br>之间的文本元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的HTML树:

i've got a HTML tree like this:

<pre id="code_block">
    This is line 1<br>
    This is line 2<br>
    This is line 3<br>
    This is line 4<span id="marker"></span><br>
    This is line 5<br>
    This is line 6<br>
    ...
</pre>

现在,我需要获取标记跨度在其中抵抗的行的文本... 我已经有了两个元素(前导< br>和< span>)

now, i need to get the text of the line where the marker span resists in... i already have the two elements (leading < br> and < span>)

$('#marker');
$('#marker').prev('br');

但是如何在两者之间获取文字?

but how to get the text between these?

我已经尝试了几种解决方案
但是问题在于两个元素之间的文本不是自己的元素...

I've already tried several solutions
but the problem is that the text between the two elements is not an own element...

想法?

推荐答案

如果要检索This is line 4,则可以这样:

If you want to retrieve This is line 4 then you can do like this:

var text = $('#marker').map(function(){
    return this.previousSibling.nodeValue;
});

alert(text[0]);

演示: http://jsfiddle.net/Bg9PT/

这篇关于jQuery:获取两个&lt; br&gt;之间的文本元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆