jQuery在< div>之间获取内容标签 [英] jQuery get content between <div> tags
问题描述
这对于某人来说很容易:
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();
这就是得到 div
并返回所有内容在里面。在此处查看更多信息: http://api.jquery.com/html/
如果您在页面上有很多 div
s并且只需要定位一个,您可以设置 id
在 div
上,并且像这样调用它
$('#whatever') .html();
其中 是 id $ c $编辑
现在你已经澄清了你的问题,一个字符串,这里是一个用vanilla js做的方法:
var l = x.length;
var y = x.indexOf('< div>');
var s = x.slice(y,l);
alert(s);
- 获取字符串的长度。
- 找出第一个
div
发生的位置 - 在那里分割内容。
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();
This says get the div
and return all the contents inside it. See more here: http://api.jquery.com/html/
If you had many div
s 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);
- get the length of the string.
- find out where the first
div
occurs - slice the content there.
这篇关于jQuery在< div>之间获取内容标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!