5

jQuery之jQuery方法总是返回jQuery对象

 1 year ago
source link: https://blogread.cn/it/article/2457?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
您现在的位置首页 --> JavaScript --> jQuery之jQuery方法总是返回jQuery对象

jQuery之jQuery方法总是返回jQuery对象

浏览:2104次  出处信息

    前几天看到一篇文章《14 Helpful jQuery Tricks, Notes, and Best Practices》感觉很不错,非常时候我这种入门级的人员,所以看懂一条翻译一条,

    jQuery方法总是返回jQuery对象:

    所以我们可以这样写(链式调用):

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是代码片段:</font><br>$someDiv.attr(’class’, ’someClass’).hide().html(’new stuff’);</td></tr></tbody></table>

    为了美观易读也可以这样写:

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是代码片段:</font><br>$someDiv.attr(’class’, ’someClass’).hide().html(’new stuff’);</td></tr></tbody></table>

    再来看下面的例子:

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是代码片段:</font><br>var someDiv = $(’#someDiv’);someDiv.hide();</td></tr></tbody></table>

    我们定义的一个元素将隐藏掉,我们可以将代码简化成:

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是代码片段:</font><br>var someDiv = $(’#someDiv’).hide();</td></tr></tbody></table>

    jquery这个特性可以减少我们遍历整个DOM的次数。我们就不用一次次的去遍历整个DOM。

建议继续学习:

QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK