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
jquery/javascript: replace string inside of html()?
提问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 »in the markup or something...
只是一个普通的老人replace应该这样做。小心?,它可能会写成»标记或其他东西......
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>»</strong>","new string");

