Day 11/16/2017 前端基础之jQuery(二)

css操作

css

 

.cs("color","red")        //改变字体颜色
.cs("border","solid red 1px")  
.cs({"border":"solid red 1px","color":"red"})

 

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("p").css("color","red");
  });
});
</script>
</head>

<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button type="button">改变段落的颜色</button>
</body>
</html>
示例
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .c1{
            border:solid red 1px;
            width: 100px;
            height:100px;
        }
    </style>
</head><body>
<div class="c1"></div>
<script src="jquery-3.2.1.min.js"></script>

<script>
var $ele=$(".c1").css("height");
console.log($ele);
</script>
</body>
</html>
取得样式属性的值:

height

$(selector).height()         //返回第一个匹配元素的高度,返回以像素计的匹配元素的高度。

$(selector).height(length)        //设置所有匹配元素的高度。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .c1{
            border:solid red 1px;
            width: 100px;
            height:100px;
        }
    </style>
</head><body>
<div class="c1"></div>
<script src="jquery-3.2.1.min.js"></script>

<script>
    //获取
//var $ele=$(".c1").height();
//console.log($ele);
    
    //设置
var $ele=$(".c1").height(300);
console.log($ele);
</script>
</body>
</html>
示例

width

 

与height类似
$(selector).width()      $(selector).width(length)

 

offset  返回或设置匹配元素相对于文档的偏移(位置)

 

 

$(selector).offset()    //返回第一个匹配元素的偏移坐标。返回第一个匹配元素的偏移坐标。


$(selector).offset({top:100,left:0})    //设置所有匹配元素的偏移坐标。

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .c1{
            border:solid red 1px;
            width: 100px;
            height:100px;
        }
    </style>
</head><body>
<div class="c1"></div>
<script src="jquery-3.2.1.min.js"></script>

<script>
var $ele=$(".c1").offset({top:20,left:20});
console.log($ele);

console.log($ele.offset());
</script>
</body>
</html>
示例

position   返回匹配元素相对于父元素的位置(偏移)

 

 

 

$(selector).position()
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .c1{
            border:solid red 1px;
            width: 100px;
            height:100px;
            position: relative;
        }
        .c2{
            border:solid blue 1px;
            width: 100px;
            height:100px;
            position: absolute;
            left: 50px;
            top: 50px;
        }
    </style>
</head><body>
<div class="c1">
    <div class="c2"></div>
</div>
<script src="jquery-3.2.1.min.js"></script>

<script>
var $ele=$(".c2").position();
console.log($ele.top,$ele.left);

</script>
</body>
</html>
示例

scrollLeft  返回或设置匹配元素的滚动条的水平位置。

$(selector).scrollLeft()        //返回第一个匹配元素的水平滚动条位置。


$(selector).scrollLeft(position)    //设置所有匹配元素的水平滚动条位置。

scrollTop  返回或设置匹配元素的滚动条的垂直位置。

 

 

$(selector).scrollTop()        

$(selector).scrollTop(offset)        

 

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
    $("div").scrollTop(100);
  });
  $(".btn2").click(function(){
    alert($("div").scrollTop()+" px");
  });
});
</script>
</head>
<body>
<div style="border:1px solid black;width:200px;height:200px;overflow:auto">
This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.
</div>
<button class="btn1">把 scroll top offset 设置为 100px</button>
<br />
<button class="btn2">获得 scroll top offset</button>
</body>
</html>
示例

innerHeight  innerWidth  outerHeight  outerWidth

innerHeight    加上上下内边距
innerWidth    加上左右内边距
outerHeight     加上上下内边距和上下边框
outerWidth     加上左右内边距和左右边框

 

 

文本操作

html()  返回或设置被选元素的内容 (inner HTML)。

$(selector).html()    //返回第一个匹配元素的内容。

$(selector).html(content)    //覆盖所有匹配元素的内容。  设置内容可以是标签

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
    </style>
</head><body>
<div class="c1">
    <p>aaaa</p>
</div>
<div class="c1">
    <p>bbb</p>
</div>

<script src="jquery-3.2.1.min.js"></script>

<script>

//    $(".c1").html()

//    $(".c1").html("Hello <b>world</b>!")
</script>
</body>
</html>
示例

text()  设置或返回被选元素的文本内容。

$(selector).text()        //返回所有匹配元素的组合的文本内容


$(selector).text(content)        //设置值时,它会覆盖被选元素的所有内容

 

HTML <select> 标签的 multiple 属性

 

multiple 属性规定可同时选择多个选项。
在不同操作系统中,选择多个选项的差异:
对于 windows:按住 Ctrl 按钮来选择多个选项
对于 Mac:按住 command 按钮来选择多个选项
由于上述差异的存在,同时由于需要告知用户可以使用多项选择,对用户更友好的方式是使用复选框。
提示:可以把 multiple 属性与 size 属性配合使用,来定义可见选项的数目。

 

<html>
<body>

<select multiple="multiple" size="2">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

</body>
</html>
示例

 

 

val()  返回或设置被选元素的值

$(selector).val()            //返回第一个匹配元素的 value 属性的值。


$(selector).val(value)            //设置 Value 属性的值
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    alert($("input:text").val());
  });
});
</script>
</head>
<body>
Firstname: <input type="text" name="fname" value="Bill" /><br />
Lastname: <input type="text" name="lname" value="Gates" /><br /><br />
<button>获得第一个文本域的值</button>
</body>
</html>
获取
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $(":text").val("Hello Kitty");
  });
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user" value="Hello World" /></p>
<button>改变文本域的值</button>
</body>
</html>
设置

attr()  设置或返回被选元素的属性值

$(selector).attr(属性名)        //返回被选元素的属性值。


$(selector).attr({属性名:"属性值"})
//设置被选元素的属性和值。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
    </style>
</head><body>


<input type="text" value="123">

<script src="jquery-3.2.1.min.js"></script>

<script>
    //获取
//    $("input").attr("type");

    //设置
    $("input").attr({egon:"aaa"});
</script>
</body>
</html>
示例

removeAttr()    // 从每一个匹配的元素中删除一个属性

 

prop 与  attr    区别

在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了。

关于它们两个的区别,网上的答案很多。这里谈谈我的心得,我的心得很简单:

对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
 

上面的描述也许有点模糊,举几个例子就知道了。 

<a href="http://www.baidu.com" target="_self" class="btn">百度</a>
 这个例子里<a>元素的DOM属性有“href、target和class",这些属性就是<a>元素本身就带有的属性,也是W3C标准里就包含有这几个属性,或者说在IDE里能够智能提示出的属性,这些就叫做固有属性。处理这些属性时,建议使用prop方法。

<a href="#" id="link1" action="delete">删除</a>
这个例子里<a>元素的DOM属性有“href、id和action”,很明显,前两个是固有属性,而后面一个“action”属性是我们自己自定义上去的,<a>元素本身是没有这个属性的。这种就是自定义的DOM属性。处理这些属性时,建议使用attr方法。使用prop方法取值和设置属性值时,都会返回undefined值。

 

再举一个例子:

<input id="chk1" type="checkbox" />是否可见
<input id="chk2" type="checkbox" checked="checked" />是否可见
像checkbox,radio和select这样的元素,选中属性对应“checked”和“selected”,这些也属于固有属性,因此需要使用prop方法去操作才能获得正确的结果。

$("#chk1").prop("checked") == false
$("#chk2").prop("checked") == true
如果上面使用attr方法,则会出现:

$("#chk1").attr("checked") == undefined
$("#chk2").attr("checked") == "checked"
详解

 

each() 方法规定为每个匹配元素规定运行的函数。

提示:返回 false 可用于及早停止循环。

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("li").each(function(){
      alert($(this).text())
    });
  });
});
</script>
</head>
<body>
<button>输出每个列表项的值</button>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>
</body>
</html>
示例

 

posted @ 2017-11-16 22:23  JAYWX  阅读(68)  评论(0编辑  收藏  举报