[英]Javascript: Replace whole anchor with text
I am using jQuery. 我正在使用jQuery。
Is there any way I can replace a anchor text with its own text in the following p tag? 有什么方法可以在下面的p标签中用自己的文本替换锚文本?
<p>
<a href="#">Some Text1</a> some text goes here
<a href="#">Some Text2</a> some other text goes here
</p>
Desired Output: 所需输出:
<p>
Some Text1 some text goes here
Some Text2 some other text goes here
</p>
Try this: 尝试这个:
$('a').replaceWith(function() {
return $(this).text();
});
Obviously, you need to make the a
selector something more specific to your needs, as I hope you're not trying to reomve all links from a page. 显然,你需要做出
a
选择更具体的东西到你的需求,我希望你不是想reomve从页面所有链接。
Try with .replaceWith()
like 尝试使用
.replaceWith()
类的
$('p a').each(function(){
$(this).replaceWith($(this).text());
});
The fastest way to strip tags is this one: 剥离标签的最快方法是:
var paragraph = $('p');
paragraph.html(paragraph.text());
If you have other tags that you want to keep, refer to Rory's answer that only replaces the a
tags. 如果还有其他标签要保留,请参考Rory的答案,该答案仅替换
a
标签。
Please try this: 请尝试以下方法:
$( document ).ready(function() {
$("p").text("Some Text1 some text goes here Some Text2 some other text goes here");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.