I am working on a google chrome extension. I extracted the source code of the webpage using a jquery get function in the following way:
$.get(link,function(data){
//From this the entire source code of the link will be available in the variable data
};
The html source will be of this format
<div class="DivClass">
<img src="2.jpg" width="100" />
<span class="time_date">FEBRUARY 19, 2014</span>
<h3><a title="Title" href="213.html">Title</a></h3>
</div>
I wanted a way to extract the href link within the divclass named DivClass. I tried the following way:
var links=[];
$(data).find('#DivClass').each(function(){
links.push($(this).attr('href'));
});
I used links as an array, as there can be multiple instances of the specific divclass. Could someone suggest where I am going wrong?
You need to use class selectors. So use .DivClass
instead of #DivClass
and You need to target anchor thus change your selectors to $(data).find('.DivClass a')
Use
var links = [];
$(data).find('.DivClass a').each(function () {
links.push($(this).attr('href'));
});
You are using id selector ( #
) for class ( .
)
$(data).find('.DivClass a').each(function(){
links.push($(this).attr('href'));
});
Because the #DivClass
is not the link. Try this:
var links=[];
$(data).find('.DivClass a').each(function(){
links.push($(this).attr('href'));
});
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.