javascript代码实例教程-ѧϰJQuery - 14

发布时间:2019-02-12 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-ѧϰJQuery - 14脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

第5章 操纵DOM

1. 操作属性


我们在第四章已经使用jQuery来读取和设置CSS,在本章我们使用jquery来操作DOM。


本节介绍一个常用的方法:attr()

javascript代码实例教程-ѧϰJQuery - 14

参数:属性名和属性&#20540;<"/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPiAgICAgICAgIMD9yOejrM7Sw8ew0c/Cw+a1XMG0vdPJz9T2vNPM4cq+PC9wPgo8cD4gICAgICAgICBqc7T6wus8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">$('p.chapter a').attr('tITle', 'external'); htML文件见附录。

当我们获取属性值的时候,必须是定义的。

js代码

window.alert($('p.square').attr('font'));

javascript代码实例教程-ѧϰJQuery - 14



如下代码即可正常运行

window.alert($('p.square').attr('class'));

javascript代码实例教程-ѧϰJQuery - 14



当我们要设置多个值时,需要使用大括号。

js代码

    $('p.chapter a').attr({   title: 'This is Demo',    id: function(index, oldValue) {return 'wikilink-' + index;}  });

我们使用Chrome来观看设置结果。按F12进入DeveloPEr Tools

javascript代码实例教程-ѧϰJQuery - 14



在HTML中有一类经常使用的便签:input。

在这个标签里都会有“value”属性,为了简便读取或者设置他,我们使用val()。

读取

VAR inputVal = $('#input1').val();
设置
$('#input1').val('0');

附:

chapter5-1.html

               Flatland            <script src="jquery-1.10.2.js"></script>     <script src="chapter05-1.js"></script>           

Flatland: A Romance of Many Dimensions

by Edwin A. Abbott

Part 1, Section 3

Concerning the Inhabitants of Flatland

an excerpt

Our PRofessional Men and Gentlemen are Squares (to which class I myself belong) and Five-sided Figures or Pentagons.

Next above these come the Nobility, of whom there are several degrees, beginning at Six-Sided Figures, or Hexagons, and From thence rising in the number of their sides till they receive the honourable title of Polygonal, or many-Sided. Finally when the number of the sides becomes so numerous, and the sides themselves so small, that the figure cannot be distinguished from a circle, he is included in the Circular or Priestly order; and this is the highest class of all.

It is a Law of Nature with us@H_304_113@ that a male child shall have one more side than his father, so that each generation shall rise (as a rule) one step in the scale of development and nobility. Thus the son of a Square is a Pentagon; the son of a Pentagon, a Hexagon; and so on.

But this rule applies not always to the TradESMan, and still less often to the Soldiers, and to the Workmen; who indeed can hardly be said to deserve the name of human Figures, since they have not all their sides equal. With them therefore the Law of Nature does not hold; and the son of an Isosceles (i.e. a Triangle with two sides equal) remains Isosceles still. Nevertheless, all hope is not such out, even from the Isosceles, that his posterity may ultiMATEly rise above his degraded condition.…

Rarely―in proportion to the vast numbers of Isosceles births―is a genuine and certifiable Equal-Sided Triangle produced from Isosceles parents. "What need of a certificate?" a Spaceland critic may ask: "Is not the procreation of a Square Son a certificate from Nature herself, proving the Equal-sidedness of the Father?" I reply that no Lady of any position will marry an uncertified Triangle. Square offspring has sometimes resulted from a slightly Irregular Triangle; but in almost every such case the Irregularity of the First generation is visited on the third; which either fails to attain the Pentagonal rank, or relapses to the TriAngular. Such a birth requires, as its antecedents, not only a series of carefully arranged intermarriages, but also a long-continued exercise of frugality and self-control on the part of the would-be ancestors of the coming Equilateral, and a patient, Systematic, and continuous development of the Isosceles Intellect through many generations.

The birth of a True Equilateral Triangle from Isosceles parents is the subject of rejoicing in our country for many furlongs round. After a strict examination conducted by the Sanitary and Social Board, the infant, if certified as Regular, is with solemn ceremonial admitted into the class of Equilaterals. He is then immediately taken from his proud yet sorrowing parents and adopted by some childless Equilateral. The Equilateral is bound by oath never to permit the child henceforth to enter his former home or so much as to look upon his relations again, for fear lest the freshly developed organism may, by force of unconscious imitation, fall back again into his hereditary level.

How admirable is the Law of Compensation! And how perfect a proof of the natural fitness and, I may almost say, the pine origin of the aristocratic constitution of the states of Flatland! By a judicious use of this Law of Nature, the Polygons and Circles are almost always able to stifle sedition in its very cradle, taking advantage of the irrePressible and boundless hopefulness of the human mind.…

Then the wretched rabble of the Isosceles, planless and leaderless, are ether transfixed without resistance by the small body of their brethren whom the Chief Circle keeps in pay for emergencies of this kind; or else more often, by means of jealousies and suspicious skillfully fomented among them by the Circular party, they are stirred to mutual warfare, and perish by one another's angles. No less than one hundred and twenty rebellions are recorded in our annals, besides minor outbreaks numbered at two hundred and thirty-five; and they have all ended thus.

Read the complete text of Flatland.


chapter05.css

/***************************************    Default Styles ***************************************/  html, body {   margin: 0;   padding: 0; }  body {   font: 62.5% Verdana, Helvetica, Arial, sans-serif;   color: #000;   background: #fff; } #container {   font-Size: 1.2em;   margin: 10px 2em; }  h1 {   font-size: 2.5em;   margin-bottom: 0; }  h2 {   font-size: 1.3em;   margin-bottom: .5em; } h3 {   font-size: 1.1em;   margin-bottom: 0; }  code {   font-size: 1.2em; }  a {   color: #06581f; }   /***************************************    Chapter Styles ***************************************/  .chapter {   margin-right: 200px; } #f-title {   font-size: 1.5em; } #excerpt {   font-style: italic; }   span.footnote {   font-style: italic;   font-family: "Times New Roman", Times, serif;   display: block;   margin: 1em 0; }  .chapter span.footnote {   display: inline; } .text-reference {   font-weight: bold; }  #notes {   margin-top: 1em;   border-top: 1px solid #dedede; } #notes li {   margin: 1em 0; }  #footer {   margin-top: 1em;   border-top: 1px solid #dedede; }   .pulled {   background: #e5e5e5;   position: absolute;   width: 120px;   top: -20px;   right: -180px;   padding: 20px;   font: italic 1.2em "Times New Roman", Times, serif; }  .rounded-top {   background: transparent url(rounded-top.png) no-repeat scroll left top;   width: 160px;   padding: 20px 0 0 0; } .rounded-bottom {   background: transparent url(rounded-bottom.png) no-repeat scroll left bottom;   width: 120px;   padding: 0 20px 20px 20px; }   .inhabitants {   border-left: 5px solid #800;   padding-left: 5px; } 

chapter05-1.js

$(document).ready(function() {     $('p.chapter a').attr({   title: 'This is Demo',    id: function(index, oldValue) {return 'wikilink-' + index;}  }); });

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-ѧϰJQuery - 14全部内容,希望文章能够帮你解决javascript代码实例教程-ѧϰJQuery - 14所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。