简体   繁体   English

页面加载后用javascript替换文本

[英]javascript replace text after the page has loaded

I want to remove/hide a piece of text that loads on my page. 我想删除/隐藏加载到页面上的一段文本。

The element doesn't have an id to relate to so I want to use a text-specific method. 该元素没有要关联的ID,因此我想使用特定于文本的方法。

Let's say the text is:"remove this line of text". 假设文本为:“删除此行文本”。
The html looks like this: html看起来像这样:

<div class="aClassName">
<p>
    <strong>remove this line of text</strong>
    ... the rest of the content.
</p>

I have tried the following: 我尝试了以下方法:

jQuery(document).ready(function($){
    document.body.innerHTML = document.body.innerHTML.replace('remove this line of text', '');
});

Didn't work. 没用 So I tried this: 所以我尝试了这个:

jQuery(document).ready(function($){
    $("body").children().each(function () {
       $(this).html( $(this).html().replace(/remove this line of text/g,"") );
    });
});

Didn't work. 没用 The idea is that after the page is loaded it removes the line. 这个想法是在页面加载后,它删除了该行。
It doesn't produces any errors as well. 它也不会产生任何错误。 Not even in the firebug. 甚至不在萤火虫中。

Anyone? 任何人?

Target Elements Based On Their Content 基于内容的目标元素

You could accomplish this using the :contains() pseudoselector in jQuery that would allow you to target certain elements based on their contents : 您可以使用jQuery中的:contains()伪选择器来完成此操作,该伪选择器将允许您根据某些元素的内容来定位它们:

$(function(){
   // This will remove any strong elements that contain "remove this line of text"
   $('strong:contains("remove this line of text")').remove();
});

You can see a working example of this here . 您可以在这里看到一个有效的示例

Broader Approach (Just Targets Elements Based On Selectors) 更广泛的方法(仅基于选择器来定位元素)

If you wanted a more simply target it by a more general selector (ie any <strong> tags that appear beneath a class called aClassName : 如果您希望通过更通用的选择器(例如,出现在名为aClassName的类下面的任何<strong>标记)更简单地将其aClassName

$('.aClassName strong').remove();

You can see an example of this approach here . 您可以在此处看到此方法的示例

I guess you can use find() and text() , ie: 我猜你可以使用find()text() ,即:

 $('.aClassName').find('strong').text("123"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="aClassName"> <p> <strong>remove this line of text</strong> ... the rest of the content. </p> 


Or simply: 或者简单地:

$('strong:contains("remove this line of text")').text("New text");

Update: 更新:

After analyzing the link supplied on the comments, you can use the following: 分析评论中提供的链接后,可以使用以下链接:

$('.payment_method_mollie_wc_gateway_ideal').find('strong').text("");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM