JQuery:使用标记选择?

发布于 2024-12-18 19:32:41 字数 1004 浏览 0 评论 0原文

我正在动态查找页面中的开放标签字符串,并希望使用 JQuery 来获取开放标签对应的元素的文本。

例如,假设这是我的页面:

<h1 class="articleHeadline">
  <NYT_HEADLINE  version="1.0" type=" ">
    The Arab Awakening and Israel
  </NYT_HEADLINE>
</h1> 
<author id="monkey" class="mainauthorstyle"> 
  <h6 class="byline">
    By 
    <a rel="author" href="http://topics.nytimes.com/top/opinion/editorialsandoped
    /oped/columnists/thomaslfriedma/index.html?inline=nyt-per" title="More Articles
    by Thomas L.Friedman" class="meta-per">
      THOMAS L. FRIEDMAN
    </a>
  </h6> 
</author>

我找到 '' 打开标记字符串,并想要获取 $("作者" id["monkey"] class["mainauthorstyle"]).text() --> 作者:托马斯·L.弗里德曼。有没有一种简单的方法可以将开放标签标记转换为选择器?我应该用正则表达式解析它吗?

编辑:我事先不知道我想要什么元素。我的代码浏览页面并以 '' 作为元素的开头。我事先不知道字面意思。我在任意网页上运行此代码。

I am dynamically finding the string of open tag within a page and want to use JQuery to get the text of the element that the open tag corresponds to.

For example, suppose this is my page:

<h1 class="articleHeadline">
  <NYT_HEADLINE  version="1.0" type=" ">
    The Arab Awakening and Israel
  </NYT_HEADLINE>
</h1> 
<author id="monkey" class="mainauthorstyle"> 
  <h6 class="byline">
    By 
    <a rel="author" href="http://topics.nytimes.com/top/opinion/editorialsandoped
    /oped/columnists/thomaslfriedma/index.html?inline=nyt-per" title="More Articles
    by Thomas L.Friedman" class="meta-per">
      THOMAS L. FRIEDMAN
    </a>
  </h6> 
</author>

I find the '<author id="monkey" class= "mainauthorstyle">' open tag string, and want to get $("author" id["monkey"] class["mainauthorstyle"]).text() --> By THOMAS L. FRIEDMAN. Is there a simple way to turn the open tag markup into a selector? Should I just parse it with regex?

Edit: I don't know beforehand what element I want. My code looks through the page and comes up with '<author id="monkey" class= "mainauthorstyle">' as the beginning of the element. I don't know the literal beforehand. I run this code on arbitrary webpages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪 2024-12-25 19:32:41

作者#monkey.mainauthorstyle

author#monkey.mainauthorstyle

故人如初 2024-12-25 19:32:41

我有点难以理解你的问题。如果您只需要文本,请使用以下命令:

$("author#monkey.mainauthorstyle").text();

如果您只想在“THOMAS L. FRIEDMAN”是作者时选择该元素,则:

$("author#monkey.mainauthorstyle:contains('THOMAS L. FRIEDMAN')")
// See http://api.jquery.com/contains-selector/ for more info on the :contains() selector

编辑后更新:

如果您能够确定要选择“ id 为“monkey”且类为“mainauthorstyle”的author 元素的“text”,您只需根据这些信息构建一个 jQuery 选择器即可。从那里您可以对生成的 jQuery 对象使用 text() 或 html() 方法来获取该元素的内容。

正如我上面的例子所示:

$("author#monkey.mainauthorstyle").text();

或者
$("author#monkey.mainauthorstyle").html();

请注意,使用的选择器可能有点过大,因为您应该能够简单地通过元素的 id 选择元素。

$("#monkey").text();

I'm having a bit of trouble understanding your question. If you just want the text, use the following:

$("author#monkey.mainauthorstyle").text();

If you want to select the element only if "THOMAS L. FRIEDMAN" is the author, then:

$("author#monkey.mainauthorstyle:contains('THOMAS L. FRIEDMAN')")
// See http://api.jquery.com/contains-selector/ for more info on the :contains() selector

UPDATED AFTER YOUR EDIT:

If you are able to determine that you want to select the "text" of the author element with id of "monkey" and class of "mainauthorstyle", you simply need to build a jQuery selector out of those bits of information. From there you can use the text() or html() methods on the resulting jQuery object to get the contents of that element.

As my example above showed:

$("author#monkey.mainauthorstyle").text();

or
$("author#monkey.mainauthorstyle").html();

Note that the selector used is probably overkill as you should be able to simply select the element by its id.

$("#monkey").text();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文