jquery/javascript:替换 html() 中的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5225987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:27:03  来源:igfitidea点击:

jquery/javascript: replace string inside of html()?

javascriptjqueryreplace

提问by matt

the following variable …

以下变量……

var breadcrumbs = $('#trail').html();

… contains:

...包含:

<href="page1/">Page 1</a> <strong>?</strong> <a href="page2">Page 2</a> <strong>?</strong> <a href="page3">Page 3</a>

<href="page1/">Page 1</a> <strong>?</strong> <a href="page2">Page 2</a> <strong>?</strong> <a href="page3">Page 3</a>

How can I replace each <strong>?</strong>with something else?

我怎样才能<strong>?</strong>用其他东西替换每个?

回答by rfunduk

Just a regular old replaceshould do it. Be careful about ?, it might be written as &raquo;in the markup or something...

只是一个普通的老人replace应该这样做。小心?,它可能会写成&raquo;标记或其他东西......

breadcrumbs.replace( new RegExp("<strong>?</strong>", 'g'), "something else" );

回答by Will

breadcrumbs.replace("<strong>?</strong>","new string");

or, depending on how you have written the ? character

或者,取决于你是如何写的?特点

breadcrumbs.replace("<strong>&raquo;</strong>","new string");