【问题标题】:Fetch the header of next div获取下一个 div 的标题
【发布时间】:2016-06-06 14:25:14
【问题描述】:

我想在单选单选按钮上获取 div 的内容,例如标题和其他详细信息。 我正在这样做

$('input[name=subscription_plan_radio]:checked').parent().next('plan-info').html()

通过这样做,我可以获取 plan-info div 的内容,但我想获取其中的下一个 h3。

所以,我正在尝试$('input[name=subscription_plan_radio]:checked').parent().next('plan-info h3').text()

但它给出了一个空白字符串。

下面是html代码。

    <div class="plan-info">
      <h3>
        Pay Per Lead
       </h3>
       <div class="plan-details">
         <h4 class="price">
             $99
            <span>month</span>
         </h4>
         <ul class="features">
           <li>Buy Unsigned Leads</li>
           <li>Price per Category from $100 to 500</li>
           <li>Email Notification when a New Leads is available</li>
        </ul>
   </div>
</div>

【问题讨论】:

  • 单选按钮在哪里?您指的是哪个标题?
  • 这台收音机在哪里?
  • 单选按钮在此 html 上方,我想获取 plan-info div 的 h3
  • 做个小提琴会很有帮助的
  • 更好的是,不要做小提琴。做一个sn-p。

标签: javascript jquery html css


【解决方案1】:

你可以这样使用它

$('input[name=subscription_plan_radio]:checked').parent().next('.plan-info').find('h3').text()

【讨论】:

  • 如何在不知道单选按钮在 DOM 中的位置的情况下使用 .parent() 和 next()?
  • 他在 plan-info div 中寻找 h3,根据他的参考,它可以在 plan-info 中获取。
  • plan-info div 中没有单选按钮,这就是 parents() 和 next() 可能不起作用的原因
【解决方案2】:

不清楚您的checkbox 在哪里。但是试试吧。

$('input[name=subscription_plan_radio]').on('change', function(){
    if($(this).checked){
        $('..plan-info').find('h3').text(); // Select only text Pay Per Lead
        $('..plan-info').find('h3').html(); // Select html <h3>Pay Per Lead</h3>
    }
})

【讨论】:

    【解决方案3】:
    $('input[name=subscription_plan_radio]:checked').parent().next().find('h3').html()
    

    应该可以工作,假设您的单选按钮位于 plan-info div 之前的 div 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-24
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多