获取没有标签的HTML文本

问题描述:

我正在尝试通过jquery访问没有HTML的DIV中的一段文本(在本例中为"12个月").我知道,如果我知道类或ID,就可以找到它,但是在这种情况下,似乎没有办法被抓住.有人可以提供潜在的解决方案吗?

I'm trying to access a piece of text inside a DIV that has no HTML through jquery (In this case the words "12 Months"). I know that I could find it if I knew the class or id, but in this case it seems it has no way to be grabbed. Can somebody provide a potential solution?

<div class="pms-subscription-plan">
<label>
<input type="radio" name="subscription_plans" data-price="19.99" data-duration="12" value="1219">$19.99 cada 12 meses</label>
<span class="pms-divider"> - </span><span class="pms-subscription-plan-price">19.99</span>
<span class="pms-subscription-plan-currency">$</span>
<span class="pms-divider"> / </span> 12 Months //This piece of text is the one I need to get and change
<div class="pms-subscription-plan-description">Si usted escogió la suscripción $19.99, se le cobrará por esa cantidad anualmente.</div>
</div>

如果有帮助,这是检查器中看到的代码的打印输出.

If it helps, here's a printout of the code as seen in the inspector.

这有效!

var a = $('.pms-subscription-plan').first().contents().filter(function() {
    return this.nodeType == 3;
}).text();

console.log($.trim(a));

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="pms-subscription-plan">
  <label>
    <input type="radio" name="subscription_plans" data-price="19.99" data-duration="12" value="1219">$19.99 cada 12 meses</label>
  <span class="pms-divider"> - </span><span class="pms-subscription-plan-price">19.99</span>
  <span class="pms-subscription-plan-currency">$</span>
  <span class="pms-divider"> / </span> 12 Months
  <div class="pms-subscription-plan-description">Si usted escogió la suscripción $19.99, se le cobrará por esa cantidad anualmente.</div>
</div>

但是您必须确保不进行重组.