使用jquery从变量值中分离img标记

时间:2014-09-06 04:03:56

标签: javascript jquery html

var str = '<div class="mino" style="overflow: hidden;"><p><a href="http://tribune.com.pk/story/758602/violence-against-children-child-homicide-rate-in-pakistan-among-worlds-worst/"><img src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/758602-assault-1409973562-427-160x120.jpg" width="160" height="120" alt=""></a></p><p></p><div><p><strong>A report released by the United Nations Children’s Fund (Unicef) on Friday revealed that one in 10 girls globally – 120 million – have been sexually abused, with violence against children taking increasingly insidious forms. The report, ‘Hidden in Plain Sight: Statistical Analysis of Violence against Children’, surveys 190 countries, including Pakistan, and surveys boys and girls aged upto 19 years.</strong></p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/013.jpg"></p></div><p>While the statistics on violent acts such as homicide are startling – Pakistan is among 10 countries with the largest number of victims of homicide among children and adolescents up to the age of 19, with close to 3000 victims in 2012 – the findings on violence against girls stands out: 30 per cent of girls aged 15 to 19 surveyed say they have experienced physical violence since the age of 15, with 24 per cent saying they experienced physical violence within the last 12 months.</p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/024.jpg"></p><p>The report acknowledges the myriad forms violent acts can take and to that end, details different kinds of violence: physical (ranging from bullying to degradation or torture and including smacks, kicking, forced ingestion or any act intended to cause some degree of pain or discomfort), sexual (including unlawful or harmful sexual activity, exploitation, forced marriages), mental (including verbal abuse, threatening, isolating or ignoring, exposure to domestic violence and cyber bullying), and neglect (the failure to adequately care for a child’s physical and psychological needs).</p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/032.jpg"></p><p>When girls were asked to identify the perpetrators of the violence, parents and caregivers (including stepmothers or stepfathers) were most commonly named, as well as husbands or partners (depending on the girls’ marital status). Among married girls, 85 per cent of Pakistani girls surveyed identified their current partner as the perpetrator of violence. One per cent listed former partners, 10 per cent listed mothers or step-mothers while 15 per cent listed mothers-in-law as perpetrators of violence. Seventeen per cent of girls said they experienced physical violence while pregnant. These girls remained silent about the abuse they suffered, with 51 per cent saying they never told anyone.</p><p>When it comes to accountability for such violence, 53 per cent of girls said a ‘husband or partner is justified in hitting or beating his wife or partner under certain circumstances’. Comparatively, 34 per cent of boys agreed that violence is condoned in certain situations.</p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/041.jpg"></p><p>The report notes that children who are abused or neglected may suffer from low self-esteem, depression and hampered development (often performing poorly at school). Witnessing violence can also cause distress, with many children internalising the nature of the violence they are exposed to and repeating patterns of abuse against others. Unicef states that the nature and impact of violence on children remains largely undocumented and underreported, while legal systems across the world fail to adequately respond to cases of violence against children.</p><p><i>Published in The Express Tribune, September 6<sup>th</sup>, 2014.</i></p><p></p><br clear="all"><p></p><em style=" color:#999; font-style:italic;"><p></p></em></div>'

我想将上述变量值中的所有img标记分开。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

在jQuery中使用字符串创建元素,然后使用find获取img元素:

var elems = $(str);
var images = $(str).find('img');
// do what you want with `images` jQuery wrapped set

答案 1 :(得分:0)

你可以尝试

var str = '<div class="mino" style="overflow: hidden;"><p><a href="http://tribune.com.pk/story/758602/violence-against-children-child-homicide-rate-in-pakistan-among-worlds-worst/"><img src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/758602-assault-1409973562-427-160x120.jpg" width="160" height="120" alt=""></a></p><p></p><div><p><strong>A report released by the United Nations Children’s Fund (Unicef) on Friday revealed that one in 10 girls globally – 120 million – have been sexually abused, with violence against children taking increasingly insidious forms. The report, ‘Hidden in Plain Sight: Statistical Analysis of Violence against Children’, surveys 190 countries, including Pakistan, and surveys boys and girls aged upto 19 years.</strong></p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/013.jpg"></p></div><p>While the statistics on violent acts such as homicide are startling – Pakistan is among 10 countries with the largest number of victims of homicide among children and adolescents up to the age of 19, with close to 3000 victims in 2012 – the findings on violence against girls stands out: 30 per cent of girls aged 15 to 19 surveyed say they have experienced physical violence since the age of 15, with 24 per cent saying they experienced physical violence within the last 12 months.</p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/024.jpg"></p><p>The report acknowledges the myriad forms violent acts can take and to that end, details different kinds of violence: physical (ranging from bullying to degradation or torture and including smacks, kicking, forced ingestion or any act intended to cause some degree of pain or discomfort), sexual (including unlawful or harmful sexual activity, exploitation, forced marriages), mental (including verbal abuse, threatening, isolating or ignoring, exposure to domestic violence and cyber bullying), and neglect (the failure to adequately care for a child’s physical and psychological needs).</p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/032.jpg"></p><p>When girls were asked to identify the perpetrators of the violence, parents and caregivers (including stepmothers or stepfathers) were most commonly named, as well as husbands or partners (depending on the girls’ marital status). Among married girls, 85 per cent of Pakistani girls surveyed identified their current partner as the perpetrator of violence. One per cent listed former partners, 10 per cent listed mothers or step-mothers while 15 per cent listed mothers-in-law as perpetrators of violence. Seventeen per cent of girls said they experienced physical violence while pregnant. These girls remained silent about the abuse they suffered, with 51 per cent saying they never told anyone.</p><p>When it comes to accountability for such violence, 53 per cent of girls said a ‘husband or partner is justified in hitting or beating his wife or partner under certain circumstances’. Comparatively, 34 per cent of boys agreed that violence is condoned in certain situations.</p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/041.jpg"></p><p>The report notes that children who are abused or neglected may suffer from low self-esteem, depression and hampered development (often performing poorly at school). Witnessing violence can also cause distress, with many children internalising the nature of the violence they are exposed to and repeating patterns of abuse against others. Unicef states that the nature and impact of violence on children remains largely undocumented and underreported, while legal systems across the world fail to adequately respond to cases of violence against children.</p><p><i>Published in The Express Tribune, September 6<sup>th</sup>, 2014.</i></p><p></p><br clear="all"><p></p><em style=" color:#999; font-style:italic;"><p></p></em></div>';

var $temp = $('<div />', {
    html: str
});
$temp.find('img').remove();
var newstr = $temp.html();

演示:Fiddle

答案 2 :(得分:0)

试试这个:

DEMO

 var str = '<div class="mino" style="overflow: hidden;"><p><a href="http://tribune.com.pk/story/758602/violence-against-children-child-homicide-rate-in-pakistan-among-worlds-worst/"><img src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/758602-assault-1409973562-427-160x120.jpg" width="160" height="120" alt=""></a></p><p></p><div><p><strong>A report released by the United Nations Children’s Fund (Unicef) on Friday revealed that one in 10 girls globally – 120 million – have been sexually abused, with violence against children taking increasingly insidious forms. The report, ‘Hidden in Plain Sight: Statistical Analysis of Violence against Children’, surveys 190 countries, including Pakistan, and surveys boys and girls aged upto 19 years.</strong></p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/013.jpg"></p></div><p>While the statistics on violent acts such as homicide are startling – Pakistan is among 10 countries with the largest number of victims of homicide among children and adolescents up to the age of 19, with close to 3000 victims in 2012 – the findings on violence against girls stands out: 30 per cent of girls aged 15 to 19 surveyed say they have experienced physical violence since the age of 15, with 24 per cent saying they experienced physical violence within the last 12 months.</p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/024.jpg"></p><p>The report acknowledges the myriad forms violent acts can take and to that end, details different kinds of violence: physical (ranging from bullying to degradation or torture and including smacks, kicking, forced ingestion or any act intended to cause some degree of pain or discomfort), sexual (including unlawful or harmful sexual activity, exploitation, forced marriages), mental (including verbal abuse, threatening, isolating or ignoring, exposure to domestic violence and cyber bullying), and neglect (the failure to adequately care for a child’s physical and psychological needs).</p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/032.jpg"></p><p>When girls were asked to identify the perpetrators of the violence, parents and caregivers (including stepmothers or stepfathers) were most commonly named, as well as husbands or partners (depending on the girls’ marital status). Among married girls, 85 per cent of Pakistani girls surveyed identified their current partner as the perpetrator of violence. One per cent listed former partners, 10 per cent listed mothers or step-mothers while 15 per cent listed mothers-in-law as perpetrators of violence. Seventeen per cent of girls said they experienced physical violence while pregnant. These girls remained silent about the abuse they suffered, with 51 per cent saying they never told anyone.</p><p>When it comes to accountability for such violence, 53 per cent of girls said a ‘husband or partner is justified in hitting or beating his wife or partner under certain circumstances’. Comparatively, 34 per cent of boys agreed that violence is condoned in certain situations.</p><p><img alt="" src="http://i1.tribune.com.pk/wp-content/uploads/2014/09/041.jpg"></p><p>The report notes that children who are abused or neglected may suffer from low self-esteem, depression and hampered development (often performing poorly at school). Witnessing violence can also cause distress, with many children internalising the nature of the violence they are exposed to and repeating patterns of abuse against others. Unicef states that the nature and impact of violence on children remains largely undocumented and underreported, while legal systems across the world fail to adequately respond to cases of violence against children.</p><p><i>Published in The Express Tribune, September 6<sup>th</sup>, 2014.</i></p><p></p><br clear="all"><p></p><em style=" color:#999; font-style:italic;"><p></p></em></div>';
            var tags = $(str).find('img');
            var imgTags = [];
            for (var i = 0; i < tags.length; i++) {
                imgTags.push(tags[i].outerHTML);
            }
            alert(imgTags);