jQuery学习笔记2——温故+知新

本文通过两个实际案例介绍了jQuery的基本用法,包括元素选择、事件处理、DOM操作等关键概念,并探讨了$()、.text()、.appendTo()等常用方法的应用。

开始之前:http://docs.jquery.com/ 是jQuery文档的网站, https://jsfiddle.net/是js的在线验证工具。

如果你没有html,CSS,js,jQuery基础,请学习它们先。

Introduction

Web pages made withHTML andCSS display static content. They don't respond to user actions like clicking a mouse or typing a key.

JavaScript andjQuery are used to make web pages interactive.

  • JavaScript is a programming language used to create web pages that change in response to user actions.

  • jQuery is a collection of prewritten JavaScript code that lets us easily create interactive effects on our site.


1. 介绍一个实际的例子,可以在https://jsfiddle.net/上得到验证。

这个例子是侧面菜单的点击滑入效果:

<html>
  <head>
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:400;300' rel='stylesheet' type='text/css'>
    <link href='style.css' rel='stylesheet'>

    
  </head>
  <body>

    <div class="menu">
      
      <!-- Menu icon -->
      <div class="icon-close">
        <img src="http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/uber/close.png">
      </div>

      <!-- Menu -->
      <ul>
        <li><a href="#">About</a></li>
        <li><a href="#">Blog</a></li>
        <li><a href="#">Help</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </div>

    <!-- Main body -->
    <div class="jumbotron">

      <div class="icon-menu">
        <i class="fa fa-bars"></i>
        Menu
      </div>
    
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="app.js"></script>
  </body>
</html>

CSS:
/* Initial body */
body {
  left: 0;
  margin: 0;
  overflow: hidden;
  position: relative;
}

/* Initial menu */
.menu {
  background: #202024 url('http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/uber/black-thread.png') repeat left top;
  left: -285px;  /* start off behind the scenes */
  height: 100%;
  position: fixed;
  width: 285px;
}

/* Basic styling */

.jumbotron {
  background-image: url('http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/uber/bg.png'); 
  height: 100%;
  -webkit-background-size: cover;
     -moz-background-size: cover;
       -o-background-size: cover;
          background-size: cover;
}

.menu ul {
  border-top: 1px solid #636366;
  list-style: none;
  margin: 0;
  padding: 0;
}

.menu li {
  border-bottom: 1px solid #636366;
  font-family: 'Open Sans', sans-serif;
  line-height: 45px;
  padding-bottom: 3px;
  padding-left: 20px;
  padding-top: 3px;
}

.menu a {
  color: #fff;
  font-size: 15px;
  text-decoration: none;
  text-transform: uppercase;
}

.icon-close {
  cursor: pointer;
  padding-left: 10px;
  padding-top: 10px;
}

.icon-menu {
  color: #fff;
  cursor: pointer;
  font-family: 'Open Sans', sans-serif;
  font-size: 16px;
  padding-bottom: 25px;
  padding-left: 25px;
  padding-top: 25px;
  text-decoration: none;
  text-transform: uppercase;
}

.icon-menu i {
  margin-right: 5px;
}
js:
var main = function(){
    $('.icon-menu').click(function(){
        $('.menu').animate({left: '0px'},200);
        $('body').animate({left: '285px'},200);
    });
    $('.icon-close').click(function(){
        $('.menu').animate({left: '-285px'},200);
        $('body').animate({left: '0px'},200);
    });
};

$(document).ready(main);


2. 一个点击显示隐藏的下拉菜单的实例,用到.children().show();

本例中有纵向排列的4个顶级菜单,每个顶级菜单在单击时可以显示它对应的下拉详情。

html总体结构:


html代码:

    <div class="articles container">
      
      <div class="article current">
        <div class="item row">
          <div class="col-xs-3">
            <p class="source">FLIGHT</p>
          </div>
          <div class="col-xs-6">
            <p class="title">Embraer adds third Legacy 500 prototype to flight test campaign</p>
          </div>
          <div class="col-xs-3">
            <p class="pubdate">Mar 23</p>
          </div>
        </div>
        <div class="description row">
          <div class="col-xs-3"> </div>
          <div class="col-xs-6">
            <h1>Embraer adds third Legacy 500 prototype to flight test campaign</h1>
            <p>The third Legacy 500 has joined Embraer's flight test programme aimed at delivering the midsize business jet in 2014. The airtcraft, serial number 003...</p>
          </div>
          <div class="col-xs-3"> </div>
        </div>
      </div>

      <div class="article">
        <div class="item row">
          <div class="col-xs-3">
            <p class="source">AW Commercial Aviation</p>
          </div>
          <div class="col-xs-6">
            <p class="title">CSeries Supplier Scramble</p>
          </div>
          <div class="col-xs-3">
            <p class="pubdate">Mar 22</p>
          </div>
        </div>
        <div class="description row">
          <div class="col-xs-3"> </div>
          <div class="col-xs-6">
            <h1>CSeries Supplier Scramble</h1>
            <p>Three months before the planned first flight of its CSeries, Bombardier is grappling with supplier issues crucial to meeting its production cost...</p>
          </div>
          <div class="col-xs-3"> </div>
        </div>
      </div>

      <div class="article">
        <div class="item row">
          <div class="col-xs-3">
            <p class="source">AW business aviation</p>
          </div>
          <div class="col-xs-6">
            <p class="title">Flying the Gulfstream G650</p>
          </div>
          <div class="col-xs-3">
            <p class="pubdate">Mar 22</p>
          </div>
        </div>
        <div class="description row">
          <div class="col-xs-3"> </div>
          <div class="col-xs-6">
            <h1>Flying the Gulfstream G650</h1>
            <p>Gulfstream is turning up the heat in the large-cabin business aircraft competition with its new G650 flagship, the largest, fastest, farthest-ranging...</p>
          </div>
          <div class="col-xs-3"> </div>
        </div>
      </div>
        <div class="item row">
          <div class="col-xs-3">
            <p class="source">FLIGHT</p>
          </div>
          <div class="col-xs-6">
            <p class="title">New retirements cut RAF VC10 fleet to four</p>
          </div>
          <div class="col-xs-3">
            <p class="pubdate">Mar 22</p>
          </div>
        </div>
        <div class="description row">
          <div class="col-xs-3"> </div>
          <div class="col-xs-6">
            <h1>New retirements cut RAF VC10 fleet to four</h1>
            <p>The UK Royal Air Force has retired another two of its Vickers VC10 tankers, with the pair's departure reducing its inventory of the Rolls-Royce...</p>
          </div>
          <div class="col-xs-3"> </div>
        </div>
      </di

    </div>
点击.article可以显示它下面的第一个 .description 的内容,使用了.children(). js code:
var main = function(){
    $('.article').click(function(){
         $(this).children('.description').show();
         //children() : nested directly under the current element.
    });
};    
$(document).ready(main);
后面再升级的js: 

var main = function(){
    $('.article').click(function(){
            $('.article').removeClass('current');//remove the current from all
            $('.description').hide();//Hide the descriptions of all
            $(this).addClass('current');//hilight the current article
            $(this).children('.description').show();//show the description of the clicked one
            //children() : nested directly under the current element.
    });
    /*Nice! Now when you click an article, it's shaded gray and its description opens up.*/
    /*Next let's see how to add two keyboard shortcuts. Here's what we want:press o, open the article; press n, open the next article*/
    //create a keypress event handler
    //The event handler should take event as a parameter.
    $(document).keypress(function(event){ 
        if (event.which === 111){//pressed 'o' key
<span style="white-space:pre">			</span>$('.description').hide();/*Hide the descriptions of all articles*/
            $('.current').children('.description').toggle();//open the on we clicked.
<span style="white-space:pre">		</span>}
        else if (event.which === 110){
            /*1.Select the current article using the '.current' selector. Save this in a new variable named currentArticle*/
            var currentArticle = $('.current');
            /*2.Select the next article using currentArticle.next(). Save this in a new variable named nextArticle*/
            var nextArticle = currentArticle.next();
            /*3.Remove the 'current' class from currentArticle using .removeClass()*/
            currentArticle.removeClass('current');
            /*4.Add the 'current' class to nextArticle using .addClass()*/
            nextArticle.addClass('current');
        }
    });
};    
$(document).ready(main);

上面这个代码作几点总结:

  • 要搞你selected的title, 先close all,再show你的那个current
  • 对current的使用,相当于一个标签,非常重要!
  • .next() 的使用方法,可参考:https://api.jquery.com/next/
  • .children() 的使用方法,非常重要
  • 最后使用了两个变量 currentArticle & nextArticle来装Object, 同时用到.next()来平移,后面的操作都是针对这两个容器来进行。
  • removeClass('active-slide');  //注意这里没有"."

最后贴出CSS代码:

body {
  background-image: url('http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/reader/bg.png');
  -webkit-background-size: cover;
     -moz-background-size: cover;
       -o-background-size: cover;
          background-size: cover;
}

p {
  margin: 0;
}

.row {
  margin: 0;
}

.articles {
  margin-top: 30px;
  margin-bottom: 30px;
}

.article {
  color: #222;
  background: rgba(255,255,255,.9);
  border-spacing: 2px;
  border-color: gray;
  font-family: arial,sans-serif;
  border-bottom: 1px #e5e5e5 solid;
}

.current .item {
  background: rgba(206,220,206,.9);
}

.item {
  cursor: pointer;
  padding-top: 7px;
  padding-bottom: 7px;
  
}

.item .source {
  margin-left: 20px;
}

.item .title {
  font-weight: bold;
}

.item .pubdate {
  margin-right: 20px;
}

.item .pubdate {
  text-align: right;  
}

.description {
  display: none;
  padding-top: 10px;
  padding-bottom: 10px;
}

.description h1 {
  margin-top: 0px;
  font-size: 23px;
}


3. 关于$( )

$( )

In jQuery, we often see $( ). It has two main uses:

  • To select existing HTML elements on the page.

    $('p') selects all p elements on the page.

  • To create new HTML elements to add to the page.

    $('<h1>') creates a new h1 element. The < > indicates that we want to create a new HTML element.

.text()

The .text() method adds text to an HTML element.

.appendTo( )  vs .prependTo( )

The .appendTo() method adds HTML elements to the end of the selected element.

$('.btn').click(function() {
  $('<li>').text('New item').appendTo('.items');
});
/**************************************/
$('.btn').click(function() {
  $('<li>').text('New item').prependTo('.items');
});
Click the New button in the web page to the right.
The .prependTo() method adds HTML elements to the beginning of the selected element.

.remove( )

Click the Delete button in the web page to the right.
The .remove() method removes the selected HTML element from the web page.

$('.btn').click(function() {
  $('.selected').remove();
});


.text( )

These six jQuery methods let you control more aspects of HTML elements:

  • .hide() hides the selected HTML element

  • .show() displays an element

  • .toggle() alternates hiding and showing an element

  • .addClass() adds a CSS class to an element

  • .removeClass() removes a class from an element

  • .toggleClass() alternates adding and removing a class from an element

4. -理解DOM Document Object Model






  • .next( )
  • .prev( )
  • .children( )
5. 操作elements in html

有很多操作html中elements的方法,详见:http://learn.jquery.com/using-jquery-core/manipulating-elements/
这里我们要说的是.appendTo(), 和 .prependTo()
以下代码实现的是响应 class= "btn" 的按钮的click动作,生成一个<li> element,然后用prependTo()操纵它放在 ul class = "posts"的列表里。

var main = function(){
    $('.btn').click(function(){   //a click event handler
        var post;
        post = $('.status-box').val();//.val() is like .text() but to Get or set the value of form elements.
        //1. use $('<li>') to create a new li element;
        //2. use .text(post) to add text to the element
        //3. use .prependTo() to prepend it to the <ul class="posts">...</ul> element.
        $('<li>').text(post).prependTo('.posts');
        //create element -> inject text -> manipulate element
    });

    //这里要更新剩余字数Count characters left
    $('.status-box').keyup(function(){ //a keyup event handler
        var postLength = $(this).val().length; //store the length of the massage typed in the status box.
        var charactersLeft = 140 - postLength;
        //update the '.counter' to show the left value
        $('.counter').text(charactersLeft);
    });
}
$(document).ready(main);





内容概要:本文主要介绍了一个基于Matlab实现的无人机空中通信仿真项目,旨在通过数值仿真手段研究无人机在空中作为通信节点时的通信性能、信号传播特性和网络拓扑行为。该仿真涵盖了无人机飞行轨迹建模、无线信道建模(如路径损耗、多普勒效应、阴影衰落等)、通信链路建立与中断判断、信号干扰分析以及网络性能评估(如吞吐量、延迟、连接可靠性等)。项目可能结合优化算法或智能控制策略,用于优化无人机位置部署或动态路径规划,以提升通信服务质量。整个仿真系统为研究人员提供了一套完整的工具链,用于验证型无人机通信协议、协作机制和网络架构的有效性。; 适合人群:具备一定Matlab编程基础和通信原理基础识,从事无人机、无线通信、网络优化等相关领域研究的研发人员和高校研究生。; 使用场景及目标:① 评估无人机作为空中基站或中继节点的通信覆盖能力和网络性能;② 设计和优化无人机集群的通信拓扑与协同策略;③ 验证型无线资源分配、移动性管理和抗干扰算法在动态空地网络中的有效性。; 阅读建议:使用者应结合Matlab代码深入理解仿真模型的构建逻辑,重点关注通信信道模块和无人机运动学模型的耦合关系,并可根据实际研究需求,对仿真参数(如环境噪声、飞行速度、天线增益)进行调整,以开展针对性的对比实验和性能分析。
内容概要:本文围绕微电网中光伏发电系统经逆变器带负载的完整仿真模型展开研究,利用Simulink平台构建了从光伏阵列建模、DC-AC逆变器控制(包括PWM调制与电压电流双闭环控制)、并网策略到负载响应的全过程仿真系统。重点分析了系统在不同工况下的动态响应特性与电能质量表现,并对并网控制策略、最大功率点跟踪(MPPT)技术及系统稳定性进行了深入探讨和验证。该模型不仅可用于教学演示微电网的基本架构与运行机制,更为科研提供了可靠的仿真平台,支持对型控制算法与系统优化方案的有效验证与评估。; 适合人群:具备一定电力电子技术、自动控制理论基础及Simulink/MATLAB操作经验的电气工程、自动化等相关专业的本科生、研究生及科研人员。; 使用场景及目标:①用于高校课程教学中微电网系统结构与运行原理的直观演示;②为科研工作者提供光伏发电并网系统的仿真验证平台,支持开展逆变器控制算法(如双闭环控制、MPPT)、系统稳定性分析及电能质量管理等关键技术的研究与优化。; 阅读建议:建议学习者结合Simulink仿真环境动手搭建模型,重点关注各功能模块间的信号传递关系与关键参数设置,并通过调整光照强度、温度、负载大小等外部条件,观察系统动态响应过程,从而深化对微电网运行特性的理解与掌握。
内容概要:本文围绕“多变量输入超前多步预测”的光伏功率预测问题,提出了一种基于CNN-BiLSTM混合深度学习模型的研究方法,并提供了完整的Matlab代码实现。该模型首先利用卷积神经网络(CNN)提取输入气象数据(如光照强度、温度、湿度等)中的局部关键特征,捕捉变量间的空间相关性;随后,通过双向长短期记忆网络(BiLSTM)充分挖掘时间序列数据中的长期依赖关系,既能利用历史信息,也能结合未来时刻的上下文信息,从而实现对未来多个时间步长的光伏功率进行高精度预测。研究重点在于处理多变量输入和满足超前多步预测的实际工程需求,有效提升了预测的准确性与鲁棒性。; 适合人群:具备一定机器学习和深度学习理论基础,熟悉Matlab编程,从事能源发电预测、电力系统调度、时间序列分析等相关领域的研究人员和工程技术人员。; 使用场景及目标:① 解决光伏出力受多重气象因素影响的复杂非线性预测问题;② 实现未来一段时间(如未来24小时)的功率超前多步预测,为电网调度、储能管理和电力市场交易提供决策依据;③ 学习和复现先进的CNN与BiLSTM融合模型在能源预测领域的具体应用。; 阅读建议:使用者应重点关注模型的网络结构设计、多变量数据预处理流程以及多步预测的实现策略。建议结合提供的Matlab代码,自行准备或替换实际的光伏电站运行数据与气象数据,通过调整模型超参数(如卷积核大小、LSTM隐藏层维度、训练周期等)进行实验,以深入理解模型性能并将其应用于具体的科研或工程项目中。
内容概要:本文介绍了一种基于Simulink的光伏储能单相逆变器并网仿真模型,系统性地实现了光伏储能系统与电网之间的能量转换与并网控制全过程。该模型涵盖逆变器的PWM调制、并网同步控制、功率调节策略以及储能单元的能量管理机制,能够精确模拟光照强度变化、负载波动及电网扰动等多种实际运行工况下的系统动态响应特性。通过模块化建模方法,模型具备良好的可扩展性与灵活性,便于研究人员对并网电能质量、控制算法性能及系统稳定性进行深入分析与优化设计。; 适合人群:具备电力电子、能源发电或自动控制等相关专业背景的本科高年级学生、研究生,以及从事光伏并网系统研发的工程技术人员。; 使用场景及目标:①作为教学工具,帮助学生理解光伏并网逆变器的工作原理与控制逻辑;②服务于科研项目,用于并网控制算法(如PI、PR、重复控制等)的设计、仿真验证与性能对比;③辅助完成毕业设计或工程项目中的系统仿真环节;④为实际工程应用提供前期仿真验证与技术预研支持。; 阅读建议:建议使用者在学习前巩固电力电子技术和可再生能源系统的基础理论,按照模型结构逐步搭建与调试;可利用文中提供的仿真框图和参数设置进行复现,并尝试引入不同工况(如光照突变、电网电压波动等)以评估系统的鲁棒性与适应性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值