简体   繁体   中英

HTML Parsing using javaScript

I am trying to get parse HTML document.

this is the HTML:

<h1>
<span class="memName fn" itemprop="name">Ankur Arora</span>
<span class="display-none" itemprop="image">http://photos1.meetupstatic.com/photos/member/3/8/f/8/member_249974584.jpeg</span>
<span class="display-none" itemprop="url">http://www.meetup.com/Meetup-API-Testing/members/191523682/</span>
</h1>

I need to get the picture and the name.

I try this code:

var name = document.querySelector("memName fn").name;

Anyone can help me? I'm new in javaScript...

Thanks

To get the inner text, you can use the text() function, like this:

HTML:

<span class="memName fn">Ankur Arora</span>

Jquery:

var memName = $(".memName").text();
console.log(memName); // Via console log
alert(memName); // Alert it

It's easy with jQuery. Just include it in your page:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

Then use .text() or .html() to extract the content of the span -elements

var pictureLink = $("span[itemprop='image']").text();
//.html() also gets the html-elements inside
var name = $("span[itemprop='name']").html();

https://jsfiddle.net/bh9mebru/

You can also use innerHTML to get the text.

<span id="memId" class="memName fn">Ankur Arora</span>

document.getElementsByClassName('memName') - This will give the list of elements having the class 'memName'

To get the first element's inner text use document.getElementsByClassName('memName')[0].innerHTML

or access by id .

document.getElementById('memId').innerHTML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM