$(“#sampleText”).html(“Hi”);
CLASS
CLASS 与 ID 非常相似,不同之处是它可以用于一个页面上的一个或多个元素。因此,尽管受到同一页面的每个元素只有一个 ID 的限制,同一页面上的多个元素仍然可以拥有相同的 CLASS。这使您可以在一个页面上跨多个元素执行函数,并且只需传入一个 CLASS 名称。
清单 7. CLASS 选择 // This will create a red background on every element on the page with a CLASS of // “redBack”. Notice that it doesn’t matter which HTML element this “redBack” // CLASS tag is attached to. Also notice the period in the front of the query // term — this is the jQuery syntax for finding the CLASS names.$(“.redBack”).css(“background”, “#ff0000″); <p class=”redBack”>This is a paragraph</p> <div class=”redBack”>This is a big div</div> <table class=”redBack”><tr><td>Sample table</td></tr></table>
|
合并搜索条件
可以在一个搜索中,将以上的 3 个搜索条件和下面的所有过滤器合并起来。通过使用 “,” 分隔每个搜索条件,搜索将返回与搜索词匹配的一组结果。
清单 8. 合并搜索
// This will hide every <p>, <span>, or <div>. $(“p, span, div”).hide();
|
更多的过滤器
虽然在 jQuery 中,这 3 个搜索参数无疑是最常用的,但还有许多其他搜索参数,可以帮助您在一个页面上快速查找所需的元素。这些过滤器以 “:” 开头,表明它们是 jQuery 搜索词中的过滤器。尽管它们也可以作为独立的搜索条件,但是设计它们的目的是将它们和以上 3 个搜索条件一起使用,从而可以调整搜索条件以找到您需要的特定元素。
清单 9. 更多的过滤器
// This will hide every <p> tag on a page $(“p”).hide();// This will hide the first element on a page, no matter its HTML tag $(“:first”).hide(); // Notice how these can be used in combination to provide more fine tuning of // search criteria. This will hide only the first <p> tag on a given page. $(“p:first”).hide();
|
可以将多个过滤器用作搜索元素。虽然在这里我没有列举所有的过滤器(这是 API 页面的任务),但其中一些过滤器在处理页面和搜索元素方面非常方便。