2012-07-19 49 views
1
<div align="left" style="margin-bottom:3px;"> 
    <b>ID:</b> 
    37749 
    <b>Active on:</b> 
    03/28/2012 
</div> 

这是我的内容,如何检索数字。使用jQuery从div中检索jQuery数据

+0

你是什么意思的数字?你的意思是'ID'字段'37749'的价值? – 2012-07-19 07:28:55

+0

是的,我想要在单独的变量中检索ID和日期 – 2012-07-19 07:31:56

+0

从纯文本中检索值不是一件简单的工作。如果将'html'结构更改为[** this **](http://jsfiddle.net/Yv2g7/)这样会更好,这样可以更容易地提取所需的信息。 – 2012-07-19 07:37:18

回答

1
var content=$('div').innerHTML(); 
var arr=content.split("</b>"); 
var ID=arr[1].split("<b>")[0]; 
var date=arr[3]; 
+0

感谢您的建议 – 2012-07-19 07:38:56

+0

此工作感谢您的建议 – 2012-07-19 07:52:43

+0

以查看网站http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_split – 2012-07-19 07:53:16

2

应该添加一些额外的HTML。

<div align="left" style="margin-bottom:3px;"> 
    <b>ID:</b> 
    <span id="id">37749</span> 
    <b>Active on:</b> 
    <span id="active-on">03/28/2012</span> 
</div> 

的jQuery:

$(function() { 
var theID = $('#id').text(), 
    activeOn = $('#active-on').text(); 
}); 
+0

正是我的想法。 – 2012-07-19 07:39:05

+0

感谢您的建议 – 2012-07-19 07:39:54

+0

但内容不在格式行内部范围 – 2012-07-19 07:41:43

2
$('div').contents().map(function() { 
    if (this.nodeType == 3 && this.nodeValue.replace(/\s/g, '').length !== 0) { 
     return this.nodeValue.replace(/\s/g, ''); 
    } 
}).toArray(); 

Demo

+0

感谢您的建议 – 2012-07-19 07:54:16