[英]javascript replace text after the page has loaded
我想刪除/隱藏加載到頁面上的一段文本。
該元素沒有要關聯的ID,因此我想使用特定於文本的方法。
假設文本為:“刪除此行文本”。
html看起來像這樣:
<div class="aClassName">
<p>
<strong>remove this line of text</strong>
... the rest of the content.
</p>
我嘗試了以下方法:
jQuery(document).ready(function($){
document.body.innerHTML = document.body.innerHTML.replace('remove this line of text', '');
});
沒用 所以我嘗試了這個:
jQuery(document).ready(function($){
$("body").children().each(function () {
$(this).html( $(this).html().replace(/remove this line of text/g,"") );
});
});
沒用 這個想法是在頁面加載后,它刪除了該行。
它也不會產生任何錯誤。 甚至不在螢火蟲中。
任何人?
基於內容的目標元素
您可以使用jQuery中的:contains()
偽選擇器來完成此操作,該偽選擇器將允許您根據某些元素的內容來定位它們:
$(function(){
// This will remove any strong elements that contain "remove this line of text"
$('strong:contains("remove this line of text")').remove();
});
您可以在這里看到一個有效的示例 。
更廣泛的方法(僅基於選擇器來定位元素)
如果您希望通過更通用的選擇器(例如,出現在名為aClassName
的類下面的任何<strong>
標記)更簡單地將其aClassName
:
$('.aClassName strong').remove();
您可以在此處看到此方法的示例 。
$('.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>
或者簡單地:
$('strong:contains("remove this line of text")').text("New text");
更新:
分析評論中提供的鏈接后,可以使用以下鏈接:
$('.payment_method_mollie_wc_gateway_ideal').find('strong').text("");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.