非包装文本的JQuery选择器

时间:2017-07-12 20:29:52

标签: javascript jquery html css jquery-selectors

我无法找到以下HTML的JQuery选择器。我试图找到&#34之后的价值;输入:"在最后关闭div之前。

到目前为止,我正在尝试:

let strokeEndAnimation: CAAnimation = {
    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = 0
    animation.toValue = 1
    animation.duration = 2

    let group = CAAnimationGroup()
    group.duration = 2.3
    group.repeatCount = .infinity
    group.animations = [animation]

    return group
}()

let strokeStartAnimation: CAAnimation = {
    let animation = CABasicAnimation(keyPath: "strokeStart")
    animation.beginTime = 0.3
    animation.fromValue = 0
    animation.toValue = 1
    animation.duration = 2

    let group = CAAnimationGroup()
    group.duration = 2.3
    group.repeatCount = .infinity
    group.animations = [animation]

    return group
}()

不在附近。

HTML:

$("div[itemscope] strong").children().text()

我想访问“酒店”这个词,就像酒店这个词一样(取决于类型)。任何帮助将不胜感激,谢谢!

3 个答案:

答案 0 :(得分:1)

转到上一个nextSibling的{​​{1}}并获取其textContent

<strong>
var txt = $("div[itemscope] strong:last")[0].nextSibling.textContent

console.log('type is:', txt)

答案 1 :(得分:1)

您可以尝试使用contents获取外部div的所有子项,然后根据NodeType进行过滤:

console.log(
  $("div[itemscope]").has("a").contents()
  .filter(function() {
    return this.nodeType === 3;
  }).text()
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div itemscope itemtype="http://schema.org/Organization">&nbsp;&nbsp;
  <a href="http://www.google.com" rel="nofollow">
    <strong>
        <font size="" color="#404040">
            <span itemprop="name">The Yarmouth Inn</span>
        </font>
    </strong></a>
  <div itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
    <span itemprop="streetAddress">&nbsp;&nbsp;30 West Street</span>,
    <span itemprop="addressLocality">Yarmouth</span>,
    <span itemprop="addressRegion">Kent</span>,
    <span itemprop="postalCode">PQ10 4TG</span>
  </div>&nbsp;&nbsp;
  <strong>Type: </strong>Hotel
</div>

答案 2 :(得分:1)

jQuery没有方法来访问特定元素中没有的文本,但可以在普通的Javascript中完成。 Remove text after element显示了如何删除这样的文本节点,类似的代码应该可以返回内容。

&#13;
&#13;
console.log($("div[itemscope] > strong:last-child").get(0).nextSibling.textContent);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div itemscope itemtype="http://schema.org/Organization">&nbsp;&nbsp; 
    <a href="http://www.google.com" rel="nofollow">
    <strong>
        <font size="" color="#404040">
            <span itemprop="name">The Yarmouth Inn</span>
        </font>
    </strong></a>
    <div itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
      <span itemprop="streetAddress">&nbsp;&nbsp;30 West Street</span>, 
      <span itemprop="addressLocality">Yarmouth</span>, 
      <span itemprop="addressRegion">Kent</span>, 
      <span itemprop="postalCode">PQ10 4TG</span> 
    </div>&nbsp;&nbsp;
    <strong>Type: </strong>Hotel
</div>
&#13;
&#13;
&#13;