DOM/[removed] get text after tag

前端 未结 6 896
Happy的楠姐
Happy的楠姐 2021-01-05 00:59

How do I get the text \"there\" that comes after a tag in an html document:

hellothere

I see that the

相关标签:
6条回答
  • 2021-01-05 01:23

    Well if you use jQuery there is a sneaky way to get this

    x = $("<p><a>hello</a>there</p>")
    x.contents()[1]

    0 讨论(0)
  • 2021-01-05 01:29

    You can use lastChild and textContent to get it: http://jsfiddle.net/M3vjR/

    0 讨论(0)
  • 2021-01-05 01:30

    You have to first get the a attribute innerhtml then take total innerhtml with using substring you can have your there part ..

    $(document).ready(function () {
    var getA=$("p >a").text();
    var getA=getA.length;
    var takeTotal=$("p").text();
    var result=takeTotal.substring(get);
    alert(result);
    });
    
    0 讨论(0)
  • 2021-01-05 01:38

    Something like this?

    var p = document.getElementsByTagName("p")[0];
    alert(p.childNodes[1].textContent)
    
    0 讨论(0)
  • 2021-01-05 01:41

    use this .. http://jsfiddle.net/2Dxy4/

    This is your HTML --

    <p id="there">
       <a>hello</a>
       there
    </p>​
    

    In your JS

    alert(document.getElementById("there").lastChild.textContent)​
    

    or

    alert(document.getElementById("there").childNodes[1].textContent)​
    
    0 讨论(0)
  • 2021-01-05 01:45

    You can find children tags of your parent tag:

    var pTag = ...
    var childNodes = pTag.childNodes;
    

    Finally you can find your text in a text node after a node.

    0 讨论(0)
提交回复
热议问题