1.DOM Core
DOM Core 并不专属于JavaScript,任何一种支持DOM的程序设计语言都可以使用它。
它的用途并非限于处理网页,也可以用来处理任何一种使用标记语言编写出来的文档,例如XML
JavaScript 中的getElemntById(),getElementsByTagName()
这些都是DOM Core的组成部分
getElemntById()
node2:/var/www/html/aa#cat a17.html
<html>
<body>
<div id='ID'>科技</div>
<script type="text/javascript" src="jquery-2.2.2.min.js"></script>
<script type="text/javascript" src="a17.js"></script>
</body>
</html>
node2:/var/www/html/aa#cat a17.js
var domObj = document.getElementById("ID");//获得DOM对象
console.log(domObj);
var ObjHTML= domObj.innerHTML;
console.log(ObjHTML);
Navigated to http://192.168.137.3/aa/a17.html
<div id="ID">
a17.js:2:1
科技
[root@node01 tmp]# cat a17.html
<html>
<body>
<div id='ID'>科技</div>
<script type="text/javascript" src="jquery-2.2.2.min.js"></script>
<script type="text/javascript" src="a17.js"></script>
</body>
</html>
[root@node01 tmp]# cat a17.pl
use LWP::UserAgent;
use POSIX;
use HTML::TreeBuilder::XPath;
use Encode;
use HTML::TreeBuilder;
use HTML::TreeBuilder::XPath;
use DBI;
use Encode;
my $tree= HTML::TreeBuilder::XPath->new;
$tree->parse_file("a17.html");
my @pageString = $tree->findvalues('//html/body/div[@id="ID"]');
print "@pageString is @pageString
";
[root@node01 tmp]# perl a17.pl
@pageString is 科技
node2:/var/www/html/aa#cat a17.js
var domObj = document.getElementsByTagName("div")[0];//获得DOM对象
var ObjHTML= domObj.innerHTML;
console.log(ObjHTML);
Navigated to http://192.168.137.3/aa/a17.html
科技
科技[root@node01 tmp]# cat a18.pl
use LWP::UserAgent;
use POSIX;
use HTML::TreeBuilder::XPath;
use Encode;
use HTML::TreeBuilder;
use Data::Dumper;
use HTML::TreeBuilder::XPath;
use DBI;
use Encode;
my $tree= HTML::TreeBuilder::XPath->new;
$tree->parse_file("a17.html");
my @pageString = $tree->findvalues('//div');
print @pageString
[root@node01 tmp]# perl a18.pl
科技[root@node01 tmp]#