获取没有标签的HTML文本 [英] Get HTML Text that has no tag

查看:364
本文介绍了获取没有标签的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>

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

这篇关于获取没有标签的HTML文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆