【问题标题】:JQuery: select using markup tags?JQuery:使用标记标签选择?
【发布时间】:2012-01-10 20:49:29
【问题描述】:

我在页面中动态查找open标签的字符串,想用JQuery获取open标签对应的元素的文本。

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

<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>

我找到'&lt;author id="monkey" class= "mainauthorstyle"&gt;' 开放标签字符串,并想得到$("author" id["monkey"] class["mainauthorstyle"]).text() --> By THOMAS L. FRIEDMAN。有没有一种简单的方法可以将打开的标签标记变成选择器?我应该用正则表达式解析它吗?

编辑:我事先不知道我想要什么元素。我的代码浏览页面并以'&lt;author id="monkey" class= "mainauthorstyle"&gt;' 作为元素的开头。我事先不知道字面意思。我在任意网页上运行此代码。

【问题讨论】:

  • 花点时间整理一下你的问题。如果您要寻求帮助,请在社区中进行一些投资。

标签: javascript jquery html jquery-selectors html-parsing


【解决方案1】:

author#monkey.mainauthorstyle

【讨论】:

    【解决方案2】:

    您的操作方法如下:http://jsfiddle.net/peduarte/MgbCX/

    【讨论】:

      【解决方案3】:

      我在理解您的问题时遇到了一些问题。如果您只想要文本,请使用以下内容:

      $("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”的作者元素的“文本”,则只需根据这些信息构建一个 jQuery 选择器。从那里您可以使用生成的 jQuery 对象上的 text() 或 html() 方法来获取该元素的内容。

      如我上面的例子所示:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2016-06-03
        • 2012-07-18
        • 2011-10-22
        • 1970-01-01
        • 1970-01-01
        • 2016-06-08
        • 2012-09-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多