Javascript jQuery获取<div>标签之间的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6854009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery get content between <div> tags
提问by user460114
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.
如何仅提取 div 标签之间的部分<div>I want this</div>
不要关注<a>
标签,因为 div 内的内容可能不同。
回答by Jason Gennaro
This is probably what you need:
这可能是你需要的:
$('div').html();
$('div').html();
This says get the div
and return all the contents inside it. See more here: http://api.jquery.com/html/
这表示获取div
并返回其中的所有内容。在此处查看更多信息: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
如果div
页面上有很多s 并且只需要定位一个,则可以在id
上设置div
并像这样调用它
$('#whatever').html();
$('#whatever').html();
where whateveris the id
无论在哪里id
EDIT
编辑
Now that you have clarified your question re this being a string, here is a way to do it with vanilla js:
现在您已经澄清了您的问题,这是一个字符串,这是一种使用 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.
- 获取字符串的长度。
- 找出第一个
div
发生的地方 - 在那里切片内容。
回答by oknoorap
jQuery has two methods
jQuery 有两种方法
// First. Get content as HTML
$("#my_div_id").html();
// Second. Get content as text
$("#my_div_id").text();
回答by ShankarSangoli
Use the below where x is the variable which holds the markup in question.
使用下面的 x 是保存相关标记的变量。
$(x).find("div").html();
回答by Dan B. Lee
Give the div a class or id and do something like this:
给 div 一个类或 id 并执行如下操作:
$("#example").get().innerHTML;
That works at the DOM level.
这适用于 DOM 级别。
回答by Suganya Shanker
Use the text method [text()
] to get text in the div element,
by identifing the element by class or id.
使用 text 方法 [ text()
] 获取 div 元素中的文本,通过按类或 id 标识元素。
回答by fatnjazzy
I suggest that you give an if to the div than:
我建议你给 div 一个 if ,而不是:
$("#my_div_id").html();
回答by ZeNo
use jquery for that:
为此使用 jquery:
$("#divId").html()
回答by Matthew Scharley
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>';
$(x).children('div').html();