使用jQuery选择所有Cite引用

Selecting all Cite references using jQuery

本文关键字:Cite 引用 jQuery 选择 使用      更新时间:2023-09-26

假设我有以下HTML:

<html>
    <head>
        <title>My Test Page</title>
    </head>
    <body>
        I liked the book <cite>Catcher In The Rye</cite>.
        I also liked the book <cite>The Great Gatsby</cite>.
    </body>
</html>

我想知道的是,是否有办法选择<cite></cite>标签之间的所有字符串?我想做的是为cite标记之间的所有文本添加一个css样式。我知道我可以为每个属性添加一个通用的class属性,并使用类名来选择它们,但有很多,我不愿意将其添加到每个属性中。jQuery有cite标签的选择器吗?

此选择器应选择文档中的所有cite标记;$('cite')

你可以这样使用它;$('cite').addClass('myCustomCssClass');

请参阅http://api.jquery.com/element-selector/

$("cite")应该获取所有实例。

在jQuery中,有三个主要的选择器:

  1. 按标签名称:$("cite")
  2. 按类别:$(".someClass")
  3. 按ID:$("#SomeID")

一个很好的想法是"我该如何定义一个CSS定义来为一个或多个元素设置样式"?一般来说,这个答案将为您提供一个在jQuery中使用的好选择器。

在您的案例中,您可以对每个CITE元素进行迭代,并获得如下所示的text/html:

$("cite").each(function() {
  var theText = $(this).text();  // example to get text
  var theHtml = $(this).html();  // example to get html
  // do whatever you want with these values
});
one liner
$('cite').addClass('yourclass').css({'height':'20px','width':'10px'}); 

依此类推