How to get a text before given element using jQuery?

前端 未结 4 1368
鱼传尺愫
鱼传尺愫 2020-12-17 14:59

Let\'s consider following html code:

Some text followed by a span element and another text followed by a bold

4条回答
  •  轮回少年
    2020-12-17 15:45

    I'm afraid you will need to use regular expressions for that. In order to get the text before span element use something like this:

    var txt = $('span').parent().html();
    var regex = /(.*)/;
    txt = txt.match(regex)[1];
    

    Modify the regex to match other parts of the string as well.

提交回复
热议问题