(2)入门指南——(7)添加jquery代码(Adding our jQuery code)
Our custom code will go in the second, currently empty, JavaScript file which we included from the HTML using <script src="01.js"></script>. For this example, we only need three lines of code, as follows:
$(document).ready(function() {
$('div.poem-stanza').addClass('highlight');
});
我们的普通的代码将书写在后面的现在还是空的js的文件中,我们在html中使用<script src="01.js"></script>把文件包含进去。比如我们只需要下面这样的三行代码:
$(document).ready(function() {
$('div.poem-stanza').addClass('highlight');
});
Finding the poem text
查找元素
The fundamental operation in jQuery is selecting a part of the document. This is done with the $()function. Typically, it takes a string as a parameter, which can contain any CSS selector expression. In this case, we wish to find all of the <div>elements in the document that have the poem-stanzaclass applied to them, so the selector is very simple. However, we will cover much more sophisticated options through the course of the book. We will step through many ways of locating parts of a document in Chapter 2, Selecting Elements.
jquery最根本的操作是查找文档的一部分,这一点使用$()方法来做到。一般来说,它使用一个字符串作为参数,其中可以包含css选择表达式。在这种场景下,我们希望查找到文档中所有有着poem-stanza类的div元素,所以选择器将会很简单。然而,我们希望通过这本书的教程,我们可以学会更加复杂的操作。我们将在第二章"查找元素"中逐步使用多种方法定位文档的部分。
When called, the $()function returns a new jQuery object instance, which is the basic building block we will be working with from now on. This object encapsulates zero or more DOM elements, and allows us to interact with them in many different ways. In this case, we wish to modify the appearance of these parts of the page, and we will accomplish this by changing the classes applied to the poem text.
当$()被调用的时候,会返回一个新的jquery对象实例,从现在开始这将是我们将要处理的基本的构建内容。这个对象封装了零个或者更多的dom元素,同时允许我们使用多种方法去影响他们。在这种情况下,我们希望修改网页中这些部分的表现形式,我们将修改附加到网页中的类的方法实现这一目标。
Injecting the new class
注入新的类
The .addClass()method, like most jQuery methods, is named self-descriptively; it applies a CSS class to the part of the page that we have selected. Its only parameter is the name of the class to add. This method, and its counterpart, .removeClass(), will allow us to easily observe jQuery in action as we explore the different selector expressions available to us. For now, our example simply adds the highlightclass, which our stylesheet has defined as italicized text with a gray background and a border.
.addClass()方法和大多数的jquery方法一样,是使用自己的功能描述来为自己命名的。他将为我们选中的网页中的那一部分添加新的类,他唯一的参数是需要被添加的类的名称。只要我们找到对我们有效的不同的选择器,这个方法和他相应的方法.removeClass()让我们可以很容易的使用jquery。现在,我们的例子只是简单的添加了一个highlight类,我们在css文件中把他定义为有着灰色背景和边框的斜体文本。
discussed, jQuery uses implicit iterationwithin methods such as .addClass(), so a
single function call is all it takes to alter all of the selected parts of the document.
• Executes functions passed to $(document).ready()even if they are added after the browser event has already occurred
• Handles the event scheduling asynchronously to allow scripts to delay it if necessary
• Simulates a DOM ready event in some older browsers by repeatedly checking for the existence of a DOM method that typically
becomes available
at the same time as the DOM
function addHighlightClass() {
$('div.poem-stanza').addClass('highlight');
}
$(document).ready(addHighlightClass);
$('div.poem-stanza').addClass('highlight');
}
$(document).ready(addHighlightClass);
$(document).ready(function() {
$('div.poem-stanza').addClass('highlight');
});
$('div.poem-stanza').addClass('highlight');
});
(2)入门指南——(7)添加jquery代码(Adding our jQuery code)的更多相关文章
- Asp.Net MVC4.0 官方教程 入门指南之三--添加一个视图
Asp.Net MVC4.0 官方教程 入门指南之三--添加一个视图 在本节中,您需要修改HelloWorldController类,从而使用视图模板文件,干净优雅的封装生成返回到客户端浏览器HTML ...
- Asp.Net MVC4.0 官方教程 入门指南之四--添加一个模型
Asp.Net MVC4.0 官方教程 入门指南之四--添加一个模型 在这一节中,你将添加用于管理数据库中电影的类.这些类是ASP.NET MVC应用程序的模型部分. 你将使用.NET Framewo ...
- AngularJS快速入门指南19:示例代码
本文给出的大部分示例都可以直接运行,通过点击运行按钮来查看结果,同时支持在线编辑代码. <div ng-app=""> <p>Name: <input ...
- 《Three.js 入门指南》3.0 - 代码构建的最基本结构。
3.0 代码构建的最基本结构 说明: 我们必需首先知道,Three.js 的一些入门级概念: 我们需要知道,OpenGL 是一套三维实现的标准,为什么说是标准,因为它是跨平台,跨语言的.甚至CAD以及 ...
- AngularJS快速入门指南20:快速参考
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- AngularJS快速入门指南18:Application
是时候创建一个真正的AngularJS单页面应用程序了(SPA). 一个AngularJS应用程序示例 你已经了解了足够多的内容来创建第一个AngularJS应用程序: My Note Save Cl ...
- AngularJS快速入门指南01:导言
AngularJS使用新的attributes扩展了HTML AngularJS对单页面应用的支持非常好(SPAs) AngularJS非常容易学习 现在就开始学习AngularJS吧! 关于本指南 ...
- Asp.Net MVC4.0 官方教程 入门指南之二--添加一个控制器
Asp.Net MVC4.0 官方教程 入门指南之二--添加一个控制器 MVC概念 MVC的含义是 “模型-视图-控制器”.MVC是一个架构良好并且易于测试和易于维护的开发模式.基于MVC模式的应用程 ...
- 【翻译转载】【官方教程】Asp.Net MVC4入门指南(2):添加一个控制器
2. 添加一个控制器 · 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-c ...
随机推荐
- duilib之源码分析
<duilib之源码分析>1 stdAfx.h * lengthof(x) 计算数组长度宏 * MAX 两值最大 * MIN 两值最小 * CLAMP(x,a,b) x在a,b之间则取 ...
- mysql 表级锁
表级锁:分为读锁和写锁: lock tables table_name read;//其他事务只能读,不能加写锁,要等待更新. SESSION 50 执行: mysql> update test ...
- linux查看接口连接状态
ethtool # ethtool em1 Settings for em1: Supported ports: [ TP ] Supported link modes: 10baseT/Half 1 ...
- 异常configure: *** apu library not found.
安装modsecurity时,出现"configure: *** apu library not found.". 解决方法: yum install apr-util- ...
- 关于js基础easy忘记的那些事儿
1.Number() 通过这个函数转化后的值仅仅有两个:数值和NaN,通过parseInt也能转化为数值.可是像"134df"转化后的值为134,而Number("134 ...
- asp.net 中将汉字转换成拼音
/// <summary> /// 获取汉字的全拼音 /// </summary> /// <param name="x">传汉字的字符串< ...
- 怎样安装配置tomcat 8
链接地址:http://jingyan.baidu.com/article/ff42efa91132a0c19e220208.html Apache tomcat 是目前最为流行的java网站开发的服 ...
- 2783: [JLOI2012]树( dfs + BST )
直接DFS, 然后用set维护一下就好了.... O(nlogn) ------------------------------------------------------------------ ...
- 基于visual Studio2013解决面试题之1003字符串逆序
题目
- attachEvent和addEventListener详解
attachEvent方法可以动态的为网页内的元素添加一个事件.通常你想为某个按扭添加一个单击事件时.你都会在按扭内写上onclick=事件名称.使用attachEvent则不必这样做.你把写好的事件 ...