简体   繁体   English

如何用“ <span>”和“</span> ” jQuery替换“(”和“)”

[英]How to replace “(” and “)” with “<span>” and “</span>” jQuery

I have follow html code: 我有遵循html代码:

<a href="#">Evente (0)</a>

an i need: 我需要:

<a href="#">Evente <span>0</span></a>

how i can change it with jQuery? 如何使用jQuery进行更改?

我认为您应该使用标准的JS正则表达式:

str.replace(/\\(/g, '<span>').replace(/\\)/, '</span>')

Don't use jQuery. 不要使用jQuery。

myAtag.innerHTML = myAtag.innerHTML.replace(/\(/,'<span>').replace(/\)/,'</span>');

Better still, output it from the server like that in the first place. 更好的是,首先从服务器将其输出。

关于jQuery功能,以下可能更简洁:

$('a').html(function(i,html){ return html.replace(/\((.*?)\)/, "<span>$1</span>")})

My thought for using jquery; 我对使用jquery的想法;

I added an ID to the achor tag to make it simpler to reference: 我在achor标签中添加了一个ID,以使其更易于引用:

<a id='target' href="#">Evente (0)</a>

And the following JQuery code 以及以下JQuery代码

$('#target').html($('#target').html().replace("(","<span>").replace(")","</span"));

Similar to both of the above answers, but with JQuery 与以上两个答案相似,但使用JQuery

谢谢SHiNKiROU现在正在运行goooood;)

$("#content-tabs a").text().replace(/\(/g, '<span>').replace(/\)/g, '</span>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM