繁体   English   中英

Javascript:将整个锚替换为文本

[英]Javascript: Replace whole anchor with text

我正在使用jQuery。

有什么方法可以在下面的p标签中用自己的文本替换锚文本?

<p>
    <a href="#">Some Text1</a> some text goes here 
    <a href="#">Some Text2</a> some other text goes here 
</p>

所需输出:

<p>
    Some Text1 some text goes here 
    Some Text2 some other text goes here 
</p>

尝试这个:

$('a').replaceWith(function() {
    return $(this).text();
});

小提琴的例子

显然,你需要做出a选择更具体的东西到你的需求,我希望你不是想reomve从页面所有链接。

尝试使用.replaceWith()类的

$('p a').each(function(){
      $(this).replaceWith($(this).text());          
});

剥离标签的最快方法是:

var paragraph = $('p');
paragraph.html(paragraph.text());

如果还有其他标签要保留,请参考Rory的答案,该答案仅替换a标签。

请尝试以下方法:

$( 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.

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