如何使用jQuery从html中提取以下文本

时间:2018-03-08 09:59:52

标签: javascript jquery

这是HTML代码..我想像这里一样提取......“具有令人印象深刻的设计,Magic Mouse 2完全可充电,因此您将无需使用传统电池。它更轻,移动部件更少,谢谢内置电池和连续底壳,并采用优化的脚部设计 - 所有这些都有助于Magic Mouse 2轨道更轻松,移动时桌面上的阻力更小。多点触控表面可让您执行简单的手势,例如在网页和滚动文档.Magic Mouse 2已准备好开箱即用,并自动与Mac配对

非常敏感的项目。“

<div id = "ctl00_ContentPlaceHolderFull_productInfoTabUC_divOverview">
<p>.text 1</p>
<p>.text 2</p>
<p>.text 3</p>
<p>.text 4</p>
<h6 class="NoBottomMargin"></h6>
"Featuring impressive design, Magic Mouse 2 is completely rechargeable, so you'll eliminate the use of traditional batteries. It's lighter, has fewer moving parts thanks to its built-in battery and continuous bottom shell, and has an optimized foot design - all helping Magic Mouse 2 track easier and move with less resistance across your desk. And the multi-touch surface allows you to perform simple gestures such as swiping between web pages and scrolling through documents. Magic Mouse 2 is ready to go right out of the box and pairs automatically with your Mac."
<br>
Very sensitive item.
<br>
<h6 class="NoBottomMargin">specifivation</h6>
<ul>
<li>Magic is in the multi-touch</li>
<li>One or two, left or right</li>
<li>Laser tracking engine</li>
</ul>
</div>

3 个答案:

答案 0 :(得分:0)

首先,您应该将文本括在<p>标记中 - 因为它包含文本并确保它只拾取特定元素 - 我在p中添加了一个类,并使用.text()定位而且你应该缩进你的代码以便于阅读。

&#13;
&#13;
var requiredText = $(".getThisText").text();
console.log(requiredText)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id = "ctl00_ContentPlaceHolderFull_productInfoTabUC_divOverview">
  <p>.text 1</p>
  <p>.text 2</p>
  <p>.text 3</p>
  <p>.text 4</p>
  <h6 class="NoBottomMargin"></h6>
  <p class="getThisText">"Featuring impressive design, Magic Mouse 2 is completely rechargeable, so you'll eliminate the use of traditional batteries. It's lighter, has fewer moving parts thanks to its built-in battery and continuous bottom shell, and has an optimized foot design - all helping Magic Mouse 2 track easier and move with less resistance across your desk. And the multi-touch surface allows you to perform simple gestures such as swiping between web pages and scrolling through documents. Magic Mouse 2 is ready to go right out of the box and pairs automatically with your Mac."
     <br>
      Very sensitive item.
     <br>
  </p>
  <h6 class="NoBottomMargin">specification</h6>
  <ul>
    <li>Magic is in the multi-touch</li>
    <li>One or two, left or right</li>
    <li>Laser tracking engine</li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以使用nextSibling。

console.log($($('.NoBottomMargin')[0].nextSibling).text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "ctl00_ContentPlaceHolderFull_productInfoTabUC_divOverview">
<p>.text 1</p>
<p>.text 2</p>
<p>.text 3</p>
<p>.text 4</p>
<h6 class="NoBottomMargin"></h6>
"Featuring impressive design, Magic Mouse 2 is completely rechargeable, so you'll eliminate the use of traditional batteries. It's lighter, has fewer moving parts thanks to its built-in battery and continuous bottom shell, and has an optimized foot design - all helping Magic Mouse 2 track easier and move with less resistance across your desk. And the multi-touch surface allows you to perform simple gestures such as swiping between web pages and scrolling through documents. Magic Mouse 2 is ready to go right out of the box and pairs automatically with your Mac."
<br>
Very sensitive item.
<br>
<h6 class="NoBottomMargin">specifivation</h6>
<ul>
<li>Magic is in the multi-touch</li>
<li>One or two, left or right</li>
<li>Laser tracking engine</li>
</ul>
</div>

答案 2 :(得分:0)

我得到了答案:

if($("[id$='divOverview'] .NoBottomMargin").length){
        $("[id$='divOverview'] .NoBottomMargin").eq(0).nextUntil('.NoBottomMargin').each(function(){
            detail.description += "\n" + $(this)[0].previousSibling.nodeValue;
        });
    }