javascript代码实例教程-(3)选择元素――(3)$()方法(The $() function)

发布时间:2019-02-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-(3)选择元素――(3)$()方法(The $() function)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 No matter which tyPE of selector we want to use in jQuery, we always start wITh the same function: $(). This function typically accepts a CSS selector as its sole parameter, and serves as a factory, returning a new jquery object pointing to the corresponding elements on the page. Just about anything that can be used in a stylesheet can also be passed as a string to this function, allowing us to apply jQuery methods to the matched set of elements.

无论我们想在jquery中使用哪种类型的选择器,我们总是使用相同的函数$()开头。这个函数典型的接受一个css选择器作为他的基础的元素,然后作为一个工厂放回一个指向网页上相应元素的的新的jquery对象。任何能被样式表使用的东西都可以作为字符串传递给这个方法,允许我们把jquery方法应用到匹配的元素上。

Making jQuery play well with other JavaScript librariesIn jQuery, the dollar sign $is simply an "alias" for jQuery. As a $()function is very common in JavaScript libraries, conflicts could arise if more than one of these libraries were being used in a given page. We can avoid such conflicts by replacing every instance of $with jQueryin our custom jQuery code. Additional solutions to this PRoblem are addressed in Chapter 10, Advanced Events.

使jquery和其他js库和谐相处

爱jquery中,美元符号$shi jquery的一个别名,然而一个$()方法在js的库中是很常见的,如果一个网页中使用了多个这些库,可能就会发生冲突。我们可以通过把我们的代码中所有的$替换成jquery来避免这样的冲突。这个问题的其他解决方案再第十章高级事件中介绍了。

 

The three Primary building blocks of selectors are tag name, ID, and class. They can be used either on their own or in combination with others. The following simple examples illustrate how these three selectors appear in code:

三种基本的选择器构建元素分别是tag name ckass和id他们可以单独使用,也可以和其他结合起来使用。下面的简单的例子说明了这三个选择器如何在代码中呈现的。

(3)选择元素――(3)$()方法(The $() function)

tag name      选择文档中所有的段落

ID                 选择文档中唯一的一个ID是some-id的元素

Class             选择文档中有着some-class这个类的元素。

As mentioned in Chapter 1, Getting Started, when we call methods of a jQuery object, the elements referred to by the selector we passed to $()are looped through automatically and implicitly. Therefore, we can usually avoid explicit iteration, such as a forloop, that is so often required in DOM scripting.

正如第一章:入门指南中介绍的那样,当我们调用jquery对象的方法的时候,我们传递给$()的选择器选中的对象将会自动隐式的循环。因此我们通常可以避免通常在DOM脚本中需要的显式调用。

Now that we have covered the basics, we're ready to start exploring some more powerful uses of selectors.

现在我们已经讲述过了基础知识,我们准备好了去开始探索一些更加高级的选择器的使用。

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

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-(3)选择元素――(3)$()方法(The $() function)全部内容,希望文章能够帮你解决javascript代码实例教程-(3)选择元素――(3)$()方法(The $() function)所遇到的问题。

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

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