当前位置: 首页 > 图文教程 > 网络编程 > Javascript > jQuery 处理网页内容的实现代码

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 jQuery 处理网页内容的实现代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 356 ::
收藏到网摘: n/a

改变页面内容应该算是Javascript最常用的功能,这包括更改已经存在的页面元素或者添加新的HTML元素。 jQuery提供两种实现这种功能的方法 – text()和html()。text()是对纯文本的处理;html()和text()相似,不同的是它还支持HTML代码。
复制代码 代码如下:

//设置ID为"b5_a"段落的内容为"这是新加入的文本信息";
$('#b5_a").text("这是新加入的文本信息");
//在ID为"b5_b"的div里加入一段html代码;
$("#b5_b").html("<p>新加入一个html段落</p>");

有时我们要读取页面的内容,这也可以用text()和html()来实现。同样,使用text()得到的是纯文本;使用html()得到的是html代码。
复制代码 代码如下:

//点击第二个按钮查看相关元素的文本内容
$("button:eq(1)").click(function(){
alert($('#b5_a').text());
});
//点击第四个按钮查看相关元素的HTML内容
$("button:eq(3)").click(function(){
alert($('#b5_a').html());
});

注意:text()和html()返回值得类型都是字符串型(string)。如果我们要对返回值进行算术运算,我们可以使用原始的JavaScript 函数:parseInt 或者 parseFloat 先把字符串转换成整形或者浮点型。
复制代码 代码如下:

<ul id="u2">
<li>12.3</li>
<li>1.5</li>
</ul>
//通过下面的jQuery代码,可以对上述列表求和
$("button:eq(4)").click(function(){
var sum = 0;
$('li').each(function(index){
sum += parseFloat($(this).text());
});
alert(sum);
});