JQuery:使用标记选择?
我正在动态查找页面中的开放标签字符串,并希望使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作者#monkey.mainauthorstyle
author#monkey.mainauthorstyle
操作方法如下: http://jsfiddle.net/peduarte/MgbCX/
Here's how you do it: http://jsfiddle.net/peduarte/MgbCX/
我有点难以理解你的问题。如果您只需要文本,请使用以下命令:
如果您只想在“THOMAS L. FRIEDMAN”是作者时选择该元素,则:
编辑后更新:
如果您能够确定要选择“ id 为“monkey”且类为“mainauthorstyle”的author 元素的“text”,您只需根据这些信息构建一个 jQuery 选择器即可。从那里您可以对生成的 jQuery 对象使用 text() 或 html() 方法来获取该元素的内容。
正如我上面的例子所示:
或者
$("author#monkey.mainauthorstyle").html();
请注意,使用的选择器可能有点过大,因为您应该能够简单地通过元素的 id 选择元素。
I'm having a bit of trouble understanding your question. If you just want the text, use the following:
If you want to select the element only if "THOMAS L. FRIEDMAN" is the author, then:
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:
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.