我在留言板上有一个很大的块或文本,其中包含以#开头的字符串。我可以使用jquery将它们替换为包含字符串的链接吗?
例如,更改:
<div id="mydiv">This is my #block of text with #multiple hash strings</div>至:
<div id="mydiv">This is my <a href="newpage.asp?block">#block</a> of text with <a href="newpage.asp?multiple">#multiple</a> hash strings</div>提前感谢!
发布于 2011-11-09 04:19:03
您可以使用以下代码替换字符串:
var string = "#has etc #has ";
string = string.replace(/#(\S+)/g, '<a href="newpage.asp?$1">#$1</a>');发布于 2011-11-09 04:20:47
这将完成以下工作:
$('*:contains("#")').each(function(){
if($(this).children().length < 1)
$(this).html(
$(this).text().replace(
/#(.*)#/
,'<a href=newpage.asp?'+$(this).text()+'>'+$(this).text()+'</a>'
)
)
});https://stackoverflow.com/questions/8056553
复制相似问题