惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 小笨笨

C# 面试题整理 《ASP.NET Core 6 框架揭秘》第五章读书笔记 - 配置选项(上) 《ASP.NET Core 6 框架揭秘》第四章读书笔记 - 文件系统 Entity Framework Core 笔记 - 入门 《ASP.NET Core 6 框架揭秘》第三章读书笔记 - 依赖注入(下) React 学习笔记之三 - 基于 Redux 状态管理 React 学习笔记之二 - React 详解 《Kafka 权威指南》之二 - 安装 Kafka 《Kafka 权威指南》读书笔记之一 - 初识Kafka React 学习笔记之一 - ES6 基础 微服务实战读书笔记-第一章 - 小笨笨 缠论 jQuery 实战读书笔记之第六章:事件本质 jQuery 实战读书笔记之第四章:使用特性、属性和数据 jQuery 实战读书笔记之第三章:操作 jQuery 集合 jQuery 实战读书笔记之第二章:选择元素 TCP/IP详解读书笔记:ARP-地址解析协议 TCP/IP详解读书笔记:链路层 TCP/IP详解读书笔记:概述 - 小笨笨
jQuery 实战读书笔记之第五章:使用 jQuery 操作页面
小笨笨 · 2018-01-27 · via 博客园 - 小笨笨

html 如下:

<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8" />
      <title>Move and Copy Lab Page - jQuery in Action, 3rd edition</title>
      <link rel="stylesheet" href="../css/main.css"/>
      <style>
         #source-pane img
         {
            margin-bottom: 1em;
         }

         .target
         {
            border: 1px solid maroon;
            background-color: #ffffcc;
            margin-bottom: 1em;
         }

         #controls div
         {
            margin-bottom: 0.5em;
         }

         button
         {
            margin-top: 10px;
         }

         #button-restore
         {
            display: none;
         }

         .done input[type=checkbox],
         .done #button-execute
         {
            display: none;
         }

         #button-restore
         {
            display: inline;
         }
      </style>
   </head>

   <body>

      <h1 class="header">jQuery Move and Copy Lab Page</h1>

      <div id="source-pane" class="box-small">
         <h2>Sources</h2>
         <div>
            <input type="checkbox" name="sources"/><br />
            <img src="images/source1.png" alt="source 1" />
         </div>

         <div>
            <input type="checkbox" name="sources"/><br />
            <img src="images/source2.png" alt="source 2" />
         </div>

         <div>
            <input type="checkbox" name="sources"/><br />
            <img src="images/source3.png" alt="source 3" />
         </div>
      </div>

      <div class="box-small">
         <h2>Target Areas</h2>

         <div>
            <div id="target-pane">
               <div id="target1" class="target">
                  <label><input type="checkbox" name="targets"/> Target 1</label>
               </div>

               <div id="target2" class="target">
                  <label><input type="checkbox" name="targets"/> Target 2</label>
               </div>

               <div id="target3" class="target">
                  <label><input type="checkbox" name="targets"/> Target 3</label>
               </div>

               <div>
                  <span>Operation:</span><br />
                  <label><input type="radio" name="operations" value="append" checked="checked"/> append</label>
                  <label><input type="radio" name="operations" value="prepend"/> prepend</label>
                  <label><input type="radio" name="operations" value="before"/> before</label>
                  <label><input type="radio" name="operations" value="after"/> after</label>
               </div>
               <div>
                  <span>Clone?:</span><br />
                  <label><input type="radio" name="clone" value="no" checked="checked"/> no</label>
                  <label><input type="radio" name="clone" value="yes"/> yes</label>
               </div>
            </div>

            <div>
               <button id="button-execute">Execute</button>
               <button id="button-restore">Restore</button>
               <button id="button-toggle">Toggle</button>
            </div>
         </div>
      </div>

      <div class="footer">
         <p>
            jQuery Selectors Lab Page - jQuery in Action, 3rd edition<br/>
            Code by Bear Bibeault, Yehuda Katz, and Aurelio De Rosa
         </p>
      </div>

      <script src="../js/jquery-3.2.1.js"></script>
      <script src="js/changeStyle.js"></script>
   </body>
</html>

修改元素的样式

/*
样式名的值是以空格分割多个值de字符串形式出现,而不是以 COM 属性数组的形式,这给处理带来了复杂性。
HTML5 引入了一个好的解决办法,通过称为 classList (样式列表)的 API 实现。
*/
 

/*
原生 js 使用 classList 增加样式(兼容新的浏览器,IE 10以上,包括 IE 10。
*/
var elements = document.getElementsByClassName('box-small');

for(var i = 0; i < elements.length; i++){
    elements[i].classList.add('target');
}

/*
addClass(names)
为集合中的元素添加指定的样式名,兼容从 IE6 开始的浏览器

name(String|Function):空格分割的多个样式名字符串;如果是函数,调用函数的上下文是当前元素(this),
参数是元素索引和元素当前样式值(所有用到的样式名,用空格分割)
*/
$('img').addClass(function(index, styleValue){
    console.log(this);
    console.log(styleValue);  //元素当前样式值(所有用到的样式名,用空格分割)
    return 'target';   //返回值作为样式名
});

$('.box-small').addClass(function(index, styleValue){
    console.log(this);
    console.log(styleValue);
    return 'target';
});

/*
removeClass(names)
从 jQuery 集合中删除每个元素指定的样式;如果是函数,调用函数的上下文是当前元素(this),参数是元素索引和要删除的
样式名(可以是以空格分割的样式名),返回值是要删除的样式名

names(String|Function)
*/
$('#target-pane > div').removeClass('target');

console.log('删除 .box-small 中的样式:')
$('.box-small').removeClass(function(index, styleName){
    console.log($(this));
    console.log(styleName);
    return 'box-small target';
});

/*
toggleClass([names][, switch])
为没有样式的元素指定添加指定的样式名,或者删除已经存在的样式

names(String|Function)
switch(Boolean)
如果设置 switch 参数,当 switch 为 true 时,那么样式通常会添加样式到没有此样式的元素上,如果为false,从元素中删除该样式
如果无参调用,集合中所有样式都会删除或恢复
如果只设置 switch 参数,集合中每个元素的样式名将根据 switch 的值保留会删除
如果提供了函数,则返回值为样式名,参数接受两个值:元素索引和元素样式的值
*/
var switchValue = true;

$('#target-pane').addClass('target');
console.log('id 为 target-pane 的 div 是否包含 target 样式:' + $('#target-pane').hasClass('target'));

$('#button-toggle').click(function(){   

    $('img').toggleClass('target');
    switchValue = !switchValue;
    console.log(switchValue);
   $('#target-pane').toggleClass('target', switchValue);

    /*
    hasClass(name)
    确定集合中的元素是否包含指定的样式
    */
    console.log('id 为 target-pane 的 div 是否包含 target 样式:' + $('#target-pane').hasClass('target'));
});

/*
直接在元素上添加样式代码(DOM 元素的 style 属性),可以覆盖预定的样式(设置了 !important 的除外)
*/

/*
css(name, value):CSS 属性的名称。接受两种 CSS 和 DOM 的属性(如 background-color 与 backgroundColor),一般使用第一种
css(properties)
为每个元素的 CSS 样式设置指定的 value

name(String)
value(String|Number|Function):如果是数字,转换为字符串在末尾加上“px”;如果是函数,接受两个参数:元素索引和当前值,返回值
为 CSS的新值
*/

/*
给按钮增加 20px 宽度(如果设置长度和宽度没有指定单位,那么默认就是 px)
*/
$('#button-toggle').css('width', '+=20');

$('p').css({
    margin: '1em',
    color: '#1933FF',
    opacity: 0.8
});

$('button').css({
    margin: '1em',
    color: '#1933FF',
    opacity: function(index, currentValue){
        return 1 - ((index % 10 + 3) / 10);
    }

});

/*
css(name)
根据指定的 name 查询集合中首个元素的 CSS 属性的计算值

name(String|Array)
返回值为字符串或属性-值对
*/
var styles = $('#source-pane').css(['border', 'background-color']);
for(var property in styles){
    console.log(property + ': ' + styles[property]);
}


/*
对于经常访问的一些小的 css 集合,jQuery 提供了便捷的方法来访问它们并返回值为常见的数据类型
*/

/*
width(value)
height(value)
设置集合中每个匹配元素的宽度和高度

value(Number|String|Function):
 */

 /*
width()
height()
查询 jQuery 对象的第一个元素的宽度和高度,单位为像素
 */
var tmpWidth = $('#source-pane').width();
console.log('#source-pane 的宽度为:' + tmpWidth);


/*
offset()
返回集合中第一个元素相对于文档的坐标,该对象(非 jQuery 对象)包含 left 和 top 属性,以像素为单位 
*/
console.log($('#button-toggle').offset());

/*
offset(coordinates)
设置集合中所有元素相对于文档的当前坐标,以像素为单位 

coordinates(Object|Function):包含 left 和 top 的对象
*/
setTimeout(function(){
    $('#button-toggle').offset({
        left: 0,
        top: 0
    })
}, 1000);

/*
position()
返回匹配集合元素中第一个元素的相对于父元素的位置(像素)
 */
console.log($('#button-toggle').position());

 /*
 注:offset() 和 position 只能用在可见元素上
 */

/*
scrollLeft()
scrollLeft(value)
scrollTop()
scrollTop(value)
*/
$('#button-restore').click(function(){
    console.log('document 滚动条的水平位置为:' + $(document).scrollLeft());
    console.log('document 滚动条的垂直位置为:' + $(document).scrollTop());

    //重回文档顶部
    setTimeout(function(){
        $(document).scrollTop(0);
    }, 1000);
});

设置元素内容

/*

样式名的值是以空格分割多个值de字符串形式出现,而不是以 COM 属性数组的形式,这给处理带来了复杂性。

HTML5 引入了一个好的解决办法,通过称为 classList (样式列表)的 API 实现。

*/

/*

原生 js 使用 classList 增加样式(兼容新的浏览器,IE 10以上,包括 IE 10。

*/

var elements = document.getElementsByClassName('box-small');

for(var i = 0; i < elements.length; i++){

elements[i].classList.add('target');

}

/*

addClass(names)

为集合中的元素添加指定的样式名,兼容从 IE6 开始的浏览器

name(String|Function):空格分割的多个样式名字符串;如果是函数,调用函数的上下文是当前元素(this),

参数是元素索引和元素当前样式值(所有用到的样式名,用空格分割)

*/

$('img').addClass(function(index, styleValue){

console.log(this);

console.log(styleValue); //元素当前样式值(所有用到的样式名,用空格分割)

return 'target'; //返回值作为样式名

});

$('.box-small').addClass(function(index, styleValue){

console.log(this);

console.log(styleValue);

return 'target';

});

/*

removeClass(names)

从 jQuery 集合中删除每个元素指定的样式;如果是函数,调用函数的上下文是当前元素(this),参数是元素索引和要删除的

样式名(可以是以空格分割的样式名),返回值是要删除的样式名

names(String|Function)

*/

$('#target-pane > div').removeClass('target');

console.log('删除 .box-small 中的样式:')

$('.box-small').removeClass(function(index, styleName){

console.log($(this));

console.log(styleName);

return 'box-small target';

});

/*

toggleClass([names][, switch])

为没有样式的元素指定添加指定的样式名,或者删除已经存在的样式

names(String|Function)

switch(Boolean)

如果设置 switch 参数,当 switch 为 true 时,那么样式通常会添加样式到没有此样式的元素上,如果为false,从元素中删除该样式

如果无参调用,集合中所有样式都会删除或恢复

如果只设置 switch 参数,集合中每个元素的样式名将根据 switch 的值保留会删除

如果提供了函数,则返回值为样式名,参数接受两个值:元素索引和元素样式的值

*/

var switchValue = true;

$('#target-pane').addClass('target');

console.log('id 为 target-pane 的 div 是否包含 target 样式:' + $('#target-pane').hasClass('target'));

$('#button-toggle').click(function(){

$('img').toggleClass('target');

switchValue = !switchValue;

console.log(switchValue);

$('#target-pane').toggleClass('target', switchValue);

/*

hasClass(name)

确定集合中的元素是否包含指定的样式

*/

console.log('id 为 target-pane 的 div 是否包含 target 样式:' + $('#target-pane').hasClass('target'));

});

/*

直接在元素上添加样式代码(DOM 元素的 style 属性),可以覆盖预定的样式(设置了 !important 的除外)

*/

/*

css(name, value):CSS 属性的名称。接受两种 CSS 和 DOM 的属性(如 background-color 与 backgroundColor),一般使用第一种

css(properties)

为每个元素的 CSS 样式设置指定的 value

name(String)

value(String|Number|Function):如果是数字,转换为字符串在末尾加上“px”;如果是函数,接受两个参数:元素索引和当前值,返回值

为 CSS的新值

*/

/*

给按钮增加 20px 宽度(如果设置长度和宽度没有指定单位,那么默认就是 px)

*/

$('#button-toggle').css('width', '+=20');

$('p').css({

margin: '1em',

color: '#1933FF',

opacity: 0.8

});

$('button').css({

margin: '1em',

color: '#1933FF',

opacity: function(index, currentValue){

return 1 - ((index % 10 + 3) / 10);

}

});

/*

css(name)

根据指定的 name 查询集合中首个元素的 CSS 属性的计算值

name(String|Array)

返回值为字符串或属性-值对

*/

var styles = $('#source-pane').css(['border', 'background-color']);

for(var property in styles){

console.log(property + ': ' + styles[property]);

}

/*

对于经常访问的一些小的 css 集合,jQuery 提供了便捷的方法来访问它们并返回值为常见的数据类型

*/

/*

width(value)

height(value)

设置集合中每个匹配元素的宽度和高度

value(Number|String|Function):

*/

/*

width()

height()

查询 jQuery 对象的第一个元素的宽度和高度,单位为像素

*/

var tmpWidth = $('#source-pane').width();

console.log('#source-pane 的宽度为:' + tmpWidth);

/*

offset()

返回集合中第一个元素相对于文档的坐标,该对象(非 jQuery 对象)包含 left 和 top 属性,以像素为单位

*/

console.log($('#button-toggle').offset());

/*

offset(coordinates)

设置集合中所有元素相对于文档的当前坐标,以像素为单位

coordinates(Object|Function):包含 left 和 top 的对象

*/

setTimeout(function(){

$('#button-toggle').offset({

left: 0,

top: 0

})

}, 1000);

/*

position()

返回匹配集合元素中第一个元素的相对于父元素的位置(像素)

*/

console.log($('#button-toggle').position());

/*

注:offset() 和 position 只能用在可见元素上

*/

/*

scrollLeft()

scrollLeft(value)

*/

console.log('restore button 滚动条的水平位置为:' + $('#button-restore').scrollLeft());

/*

scrollTop()

scrollTop(value)

*/

console.log('restore button 滚动条的垂直位置为:' + $('#button-restore').scrollTop());