从DIV元素

时间:2018-06-19 09:31:37

标签: jquery

我正在尝试从以下标记中删除RT。我试过了

 $('#tweet').text( $('#tweet').text().replace("RT ", ''));     

但它只从第一个div中删除了RT。有什么想法吗?

<div class="fslider" data-animation="slide" data-arrows="false" data-pagi="false">
  <div class="flexslider">
    <div class="slider-wrap">
      <div class="slide" id="tweet">RT  A new empowerment group, the Business Economic Empowerment Forum, has been launched in Zim aiming to promote business and ec…<span class="twitter_date"><br>
        </span></div>
      <div class="slide" id="tweet">RT  Alexander Forbes extends footprint to Zim through its acquisition of a significant stake in African Actuarial Consultants<span class="twitter_date"><br>
        </span></div>
      <div class="slide" id="tweet">RT  Nigeria &amp;amp; Morocco sign agreements on a regional gas pipeline which will see Nigeria providing gas to countries in West Africa<span class="twitter_date"><br>
        </span></div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:4)

id必须在页面上是唯一的,因此第一步是更改标记以使用tweet类而不是id:< / p>

<div class="fslider" data-animation="slide" data-arrows="false" data-pagi="false">
  <div class="flexslider">
    <div class="slider-wrap">
      <div class="slide tweet">RT  A new empowerment group, the Business Economic Empowerment Forum, has been launched in Zim aiming to promote business and ec…<span class="twitter_date"><br>
        </span></div>
      <div class="slide tweet">RT  Alexander Forbes extends footprint to Zim through its acquisition of a significant stake in African Actuarial Consultants<span class="twitter_date"><br>
        </span></div>
      <div class="slide tweet">RT  Nigeria &amp;amp; Morocco sign agreements on a regional gas pipeline which will see Nigeria providing gas to countries in West Africa<span class="twitter_date"><br>
        </span></div>
    </div>
  </div>
</div>

然后,您需要使用text's callback

循环遍历它们
$(".tweet").text(function(_, text) {
    return text.replace("RT ", "");
});

请注意,奇怪的是,文本是回调的第二个参数。

直播示例:

&#13;
&#13;
$(".tweet").text(function(_, text) {
    return text.replace("RT ", "");
});
&#13;
<div class="fslider" data-animation="slide" data-arrows="false" data-pagi="false">
  <div class="flexslider">
    <div class="slider-wrap">
      <div class="slide tweet">RT  A new empowerment group, the Business Economic Empowerment Forum, has been launched in Zim aiming to promote business and ec…<span class="twitter_date"><br>
        </span></div>
      <div class="slide tweet">RT  Alexander Forbes extends footprint to Zim through its acquisition of a significant stake in African Actuarial Consultants<span class="twitter_date"><br>
        </span></div>
      <div class="slide tweet">RT  Nigeria &amp;amp; Morocco sign agreements on a regional gas pipeline which will see Nigeria providing gas to countries in West Africa<span class="twitter_date"><br>
        </span></div>
    </div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

请注意,replace无论文本在何处都会替换RT,并且只会替换第一个return text.replace(/^RT /, ""); 。要使其专门只替换文本开头的一个,如果有的话,将其更改为

^

...其中/^RT +/表示&#34;开始输入。&#34;您甚至可以使用$(".tweet").text(function(_, text) { return text.replace(/^RT +/, ""); });,因此您可以替换它后面的所有空格而不只是一个。

直播示例:

&#13;
&#13;
<div class="fslider" data-animation="slide" data-arrows="false" data-pagi="false">
  <div class="flexslider">
    <div class="slider-wrap">
      <div class="slide tweet">RT  A new empowerment group, the Business Economic Empowerment Forum, has been launched in Zim aiming to promote business and ec…<span class="twitter_date"><br>
        </span></div>
      <div class="slide tweet">RT  Alexander Forbes extends footprint to Zim through its acquisition of a significant stake in African Actuarial Consultants<span class="twitter_date"><br>
        </span></div>
      <div class="slide tweet">RT  Nigeria &amp;amp; Morocco sign agreements on a regional gas pipeline which will see Nigeria providing gas to countries in West Africa<span class="twitter_date"><br>
        </span></div>
    </div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
&#13;
id
&#13;
&#13;
&#13;