我想要進行以下操作:從文本輸入中獲取值,然後檢查是否在整個文檔中出現第一個值。如果它出現在文檔中,我想用第二個變量的值替換它的所有實例。腳本將變量替換爲文檔中的值
Snipet中的代碼不正確導致函數替換不起作用... 有沒有人知道如何解決它?
預先感謝您!
$(document).ready(function(){
var replaced = $('#replaced').val();
var modified = $('#modified').val();
$('#submit').click(function(){
if($(".entireSite:contains(" + replaced + ")").text() === '') {
alert('there is no wanted word/phrase at the website');
} else {
$('.entireSite').text().replace(replaced, modified);
};
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="text" id="replaced" value="word/phrase to modify"><br><br>
<input type="text" id="modified" value="new word/phrase"><br><br>
<input type="submit" value="submit" id="submit">
<div class="entireSite">
<p>Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Maecenas sed sollicitudin
lectus. Sed libero tellus, sodales quis eros
eu, vestibulum interdum nibh. Nunc Lorem quis
diam vitae enim finibus pretium ut a ipsum.
Cras ut ullamcorper mi, vel fringilla augue.
Pellentesque Lorem consequat vulputate ipsum
sit amet faucibus. Aliquam erat orci, cursus
id tempor bibendum, condimentum vitae enim.</p>
</div>
</body>
</html>
謝謝你的支持。它工作正常! – Bix