网站制作学习网JS脚本→正文:jquery属性操作2
字体:

jquery属性操作2

JS脚本 2010/8/25 15:08:56  点击:不统计

学习www.网for站asp制.cn作

参考共用代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>jquery属性操作</TITLE>
<script language="javascript" src="jquery-1.4.2.min.js"></script>
<SCRIPT language="javascript">
$(function(){
<!--jquery文档处理代码区-->
});
</SCRIPT>
<style type="text/css">
.img {height:50px;}
</style>
<BODY>
<div>
<ul>
<li id="foo">foo</li>
<li id="bar">bar</li>
<li id="baz">baz</li>
</ul>
</div>
<div id="foraspcn" name="xxx">
<p>http://www.forasp.cn</p>
<p><img id="forasp" src="forapslogo.jpg" height="100" width="200" border="0"></p>
<input type="text" id="jquery" name="jquery" value="jquery属性操作">
<select id="selectname">
</select>
</div>
</BODY>
</HTML>
3.HTML属性的操作
(1).HTML();取得第一个匹配元素的html内容。这个函数不能用于XML文档。但可以用于XHTML文档
举例:$("ul").html();结果<li id="foo">foo</li><li id="bar">bar</li><li id="baz">baz</li>
(2).HTML("content"); 设置对象的内容为html content。
举例:$("#foraspcn").html("<b>this is www.forasp.cn</b>"); 结果:<div id="foraspcn" name="xxx"><b>this is www.forasp.cn</b></div>,这里展示的内容将是粗体,不显示<b>
(3).html(function(index, html));设置每一个匹配元素的html内容。这个函数不能用于XML文档。但可以用于XHTML文档。在参数里的index是索引,html是原来的值。
4.text文本内容的操作
(1).text(); 获取对象里面的包含文本内容,对html和xml都有效
举例:$("#foraspcn").text(); 结果http://www.forasp.cn
(2).text("content");设置对轩昂里面包含的内容为content,但是html不会转换
举例$("#foraspcn").text("<b>this is www.forasp.cn</b>"); 结果<div id="foraspcn" name="xxx"><b>this is www.forasp.cn</b></div>,这里展示的内容将是原样(不加粗),并且显示出来<b>;
(3).text(function(index,text){});与 html() 类似, 但将编码 HTML (将 "<" 和 ">" 替换成相应的HTML实体).index为元素在集合中的索引位置,text为原先的text值
5.对值的操作
(1).val(); 获取对象的值;包括select。如果多选,将返回一个数组,其包含所选的值
$("#jquery").val(); 结果: jquery属性操作,如果是select则结果为选中值
(2).val("content");设置对象的值为content
$("#jquery").val("第八课jquery");结果:<input type="text" id="jquery" name="jquery" value="jquery属性操作">
(3).val(array); check,select,radio等都能使用为之赋值,array 为select/check等值。
$("#selectname").val("2"); 或者$("#selectname").val(["2","3"]);前面的在单选select时,值为2的option选中,后面的当select为多选时,值为2和3的选中。
(4).val(function(index, value));此函数返回一个要设置的值。接受两个参数,index为元素在集合中的索引位置,text为原先的text值
举例:
$('#jquery').val(function() {return this.value + ' ' + this.Name;});设置id为jquery的值为当前值+name属性值。

jquery属性操作,学习结束


·上一篇:jquery属性操作 >>    ·下一篇:jQuery操作Frame(iframe)网页框架 >>
推荐文章
最新文章