jQuery在< div>之间获取内容标签 [英] jQuery get content between <div> tags

查看:153
本文介绍了jQuery在< div>之间获取内容标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对于某人来说很容易:

  var x ='< p> blah< / p>< lt ; div>< a href =http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=brd&FlightID=2997227&Page=&PluID=0&Pos=9088target = _blank>< img src =http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=bsr&FlightID=2997227&Page=&PluID=0&Pos=9088border = 0 width = 300 height = 250>< / a>< / div>'; 

如何提取div标签< div>我想要这个< / div>
不要关注< a> 标签,因为内容可能与div。

解决方案

这可能是您需要的: code> $('div')。html();



demo



这就是得到 div 并返回所有内容在里面。在此处查看更多信息: http://api.jquery.com/html/



如果您在页面上有很多 div s并且只需要定位一个,您可以设置 id div 上,并且像这样调用它

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



其中 id



现在你已经澄清了你的问题,一个字符串,这里是一个用vanilla js做的方法:

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

演示此处


  1. 获取字符串的长度。

  2. 找出第一个 div 发生的位置

  3. 在那里分割内容。


This'll probably be easy for someone:

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>';

How do I extract only the portion between the div tags <div>I want this</div> Don't focus on the <a> tag as the content could be different inside the div.

解决方案

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.

这篇关于jQuery在&lt; div&gt;之间获取内容标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆