Parsing of html string using jquery

前端 未结 7 1679
[愿得一人]
[愿得一人] 2020-12-01 01:41

I am trying to parse this html through jQuery to get data1, data2, data3. While I do get data2 and data3 I am unable to get data3 with my approach. I am fairly new to jQuery

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 02:17

    I think the main problem is that you cannot have an html to your jquery. In your case what happens to Jquery is that it tries to find the first html tag, That in your case is the div with class0.

    Test this to see that I am right:

    if($(datahtml).hasClass('class0'))
        alert('Yes you are right :-)');
    

    So this means that you cannot add the html and or the body tag as a part to have a query within.

    If you want to make it work just try to add this part of code:

    data1

    data2

    data3

    So try this:

    var datahtml = "

    data1

    data2

    data3

    "; alert($(datahtml).find(".class0").text()); // work alert($(datahtml).find(".class1").text()); // work alert($(datahtml).find("#mydivid").text()); // work

提交回复
热议问题