3   操作元素—属性 CSS 和 文档处理 


  • 3.1 属性操作

$("p").text()    $("p").html()   $(":checkbox").val()

$(".test").attr("alex")   $(".test").attr("alex","sb")

$(".test").attr("checked","checked")  $(":checkbox").removeAttr("checked")

$(".test").prop("checked",true)

$(".test").addClass("hide")


  • 3.1.1、html text val 

代码展示:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery-1.12.4.js"></script>
</head>
<body>

<script>

// html([val|fn])
$('p').html();
$("p").html("Hello <b>world</b>!");
$("p").html(function(n){
return "这个 p 元素的 index 是:" + n;
});

// text() text([val|fn]) 取得所有匹配元素的内容。
$('p').text();
$("p").text("Hello world!");
$("p").text(function(n){
return "这个 p 元素的 index 是:" + n;
});
// val([val|fn|arr])
$("input").val();
$("input").val("hello world!");
$('input:text.items').val(function() {
return this.value + ' ' + this.className;
});

</script>
</body>
</html>

html text val


  • 3.1.2、属性
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery-1.12.4.js"></script>
</head>
<body>

<script>

// html([val|fn])
$('p').html();
$("p").html("Hello <b>world</b>!");
$("p").html(function(n){
return "这个 p 元素的 index 是:" + n;
});

// text() text([val|fn]) 取得所有匹配元素的内容。
$('p').text();
$("p").text("Hello world!");
$("p").text(function(n){
return "这个 p 元素的 index 是:" + n;
});
// val([val|fn|arr])
$("input").val();
$("input").val("hello world!");
$('input:text.items').val(function() {
return this.value + ' ' + this.className;
});

</script>
</body>
</html>



  • 3.1.3、class css 类

代码展示:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../jquery-1.9.1.min.js"></script>
</head>
<body>
<ul>
<li>Hello</li>
<li>Hello</li>
<li>Hello</li>
</ul>
<strong>jQuery 代码:</strong>
<script>

// addClass(class|fn) addClass 为每个匹配的元素添加指定的类名。
$("p").addClass("selected");
$("p").addClass("selected1 selected2");

$('ul li:last').addClass(function() {
return 'item-' + $(this).index();
});

// removeClass([class|fn])
$("p").removeClass("selected"); //从匹配的元素中删除 'selected' 类

//删除匹配元素的所有类
$("p").removeClass();
//删除最后一个元素上与前面重复的class
$('li:last').removeClass(function() {
return $(this).prev().attr('class');
});

//toggleClass(class|fn[,sw]) 如果存在(不存在)就删除(添加)一个类。

//根据父元素来设置class属性
$('div.foo').toggleClass(function() {
if ($(this).parent().is('.bar') {
return 'happy';
} else {
return 'sad';
}
});

//每点击三下加上一次 'highlight' 类
var count = 0;
$("p").click(function(){
$(this).toggleClass("highlight", count++ % 3 == 0);
});

</script>
</body>
</html>



3.2、 CSS操作
        3.2.1(样式)   css("{color:'red',backgroud:'blue'}") 
        3.2.2(位置)   offset()    position()  scrollTop()  scrollLeft()    
        3.2.3(尺寸)   height()  width()  
        
  • 例子详见【滚动菜单-案例】  滚动菜单 【点击链接】


3.3 文档处理

       内部插入  $("#c1").append("<b>hello</b>")     $("p").appendTo("div")
                      prepend()    prependTo()

       外部插入  before()  insertBefore()  after insertAfter()
                       replaceWith()   remove()  empty()  clone()




3 、操作元素 (属性 CSS 和 文档处理)的更多相关文章

  1. mongodb对数组元素及内嵌文档进行增删改查操作(转)

    from:https://my.oschina.net/132722/blog/168274 比如我有一个user类,他包含一个标签属性,这个标签是一个数组,数组里面的元素是内嵌文档,格式如下: &l ...

  2. MongoDB对数组元素及内嵌文档进行增删改查操作

    比如我有一个user类,他包含一个标签属性,这个标签是一个数组,数组里面的元素是内嵌文档,格式如下: {    "_id" : "195861",    &qu ...

  3. css参考文档; 官方英文说明!! 1 margin padding 百分比参照物 2 margin值为auto时的说明 3 div在div里垂直居中方法 4 dispaly:flex说明

    css参考文档        http://css.doyoe.com/ 两篇很好的文章:(下面的css官方英文说明链接 有时间可以研究下 http://www.w3.org/TR/css3-box/ ...

  4. css标准文档流

    css标准文档流 所谓的标准文档流指的是网页当中的一个渲染顺序,就如同人类读书一样,从上向下,从左向右.网页的渲染顺序也是如此.而我们使用的标签默认都是存在于标准文档流当中. 标准文档流当中的特性 空 ...

  5. [中文版] 可视化 CSS References 文档

    本文分享了我将可视化 CSS References 文档翻译成中文版的介绍,翻译工作还在陆续进行中,供学习 CSS 参考. 1. 可视化 CSS References 文档介绍 许多 CSS 的文档都 ...

  6. css脱离文档流

    作者:张秋怡链接:http://www.zhihu.com/question/24529373/answer/29135021来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  7. 使用jQuery操作元素属性

    在jQuery中,提供了attr函数来操作元素属性,具体如下: 函数名 说明 例子 attr(name) 取得第一个匹配元素的属性值. $("input").attr(" ...

  8. 003——VUE操作元素属性

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. vue.js操作元素属性

    vue动态操作div的class 看代码: <!doctype html> <html lang="en"> <head> <meta c ...

随机推荐

  1. [java ] java.util.zip.ZipException: error in opening zip file

    严重: Failed to processes JAR found at URL [jar:file:/D:/tools/apache-tomcat-7.0.64_2/webapps/bbs/WEB- ...

  2. 《C++ Primer Plus》第11章 使用类 学习笔记

    本章介绍了定义和使用类的许多重要方面.一般来说,访问私有类成员的唯一方法是使用类方法.C++使用友元函数来避开这种限制.要让函数称为友元,需要在类声明中声明该函数,并在声明前加上关键字friend.C ...

  3. 通过([AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)] )在前台html页面调用cs方法

    app_code中CS代码( Cs页面文件名public class ajaxGET): [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement ...

  4. 【Spring Boot && Spring Cloud系列】构建Springboot项目 实现restful风格接口

    项目代码如下: package hello; import org.springframework.boot.SpringApplication; import org.springframework ...

  5. 使用MAT分析Java内存

    Overview MAT(Memory Analyzer Tool) 是一个JAVA Heaper分析器,可以用来分析内存泄露和减少内存消耗.分析Process showmap中的/dev/ashme ...

  6. nutch 1.7导入Eclipse

    1.下载Nutch1.7的包 apache-nutch-1.7-src.zip,解压之后应该包括 bin,conf,src等目录 2.将解压之后的 apache-nutch-1.7 文件夹放到ecli ...

  7. AB压力测试工具

    1.安装AB工具: yum install httpd-tools 2.测试: ab -n -c http://localhost.com/ 其中-n表示请求数,-c表示并发数 3.测试结果 [roo ...

  8. 微信小程序 --- 获取当前坐标

    获取位置:get.location type:wgs84(是全球定位系统,获取的坐标,gcj02是国家测绘局给出的坐标) btnclick:function(){ wx.getLocation({ t ...

  9. 设备信息的管理(Device) ---- HTML5+

    模块:Device Device模块管理设备信息,用于获取手机设备的相关信息,如IMEI.IMSI.型号.厂商等.通过plus.device获取设备信息管理对象. 应用场景:打电话,铃声提醒,震动提醒 ...

  10. 网络费用流-最小k路径覆盖

    多校联赛第一场(hdu4862) Jump Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...