jQuery get content between
tags

前端 未结 8 1333
情歌与酒
情歌与酒 2020-12-05 04:10

This\'ll probably be easy for someone:

var x = \'

blah

相关标签:
8条回答
  • 2020-12-05 04:34
    var x = '<p>blah</p><div><a href="http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=brd&FlightID=2997227&Page=&PluID=0&Pos=9088" target="_blank"><img src="http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=bsr&FlightID=2997227&Page=&PluID=0&Pos=9088" border=0 width=300 height=250></a></div>';
    $(x).children('div').html();
    
    0 讨论(0)
  • 2020-12-05 04:35

    Use the text method [text()] to get text in the div element, by identifing the element by class or id.

    0 讨论(0)
  • 2020-12-05 04:37

    This is probably what you need:

    $('div').html();

    demo

    This says get the div and return all the contents inside it. See more here: http://api.jquery.com/html/

    If you had many divs on the page and needed to target just one, you could set an id on the div and call it like so

    $('#whatever').html();

    where whatever is the id

    EDIT

    Now that you have clarified your question re this being a string, here is a way to do it with vanilla js:

    var l = x.length;
    var y = x.indexOf('<div>');
    var s = x.slice(y,l);
    alert(s);
    

    Demo Here

    1. get the length of the string.
    2. find out where the first div occurs
    3. slice the content there.
    0 讨论(0)
  • 2020-12-05 04:45

    I suggest that you give an if to the div than:

    $("#my_div_id").html();
    
    0 讨论(0)
  • 2020-12-05 04:49

    Use the below where x is the variable which holds the markup in question.

    $(x).find("div").html();
    
    0 讨论(0)
  • 2020-12-05 04:50

    Give the div a class or id and do something like this:

    $("#example").get().innerHTML;
    

    That works at the DOM level.

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