如果在B类中找到一个文本,则用替换函数中的C text和html替换它

时间:2016-01-25 10:28:08

标签: jquery

<script type="text/javascript">
    jQuery(document).ready(function() {
        var d = jQuery('.bbp-template-notice p');
        d.text(d.text().trim().replace(/You must be logged in to create new topics./i, "You must be logged in to create new topics. Please  <a href='#pagelink'><font style='font-weight:bold;'>LOGIN HERE</font></a>"));
    });
</script>

现在它显示为:

  

您必须登录才能创建新主题。请在这里登录&#34;

它正在前端显示html代码。我希望anchor属性在该类中工作。

1 个答案:

答案 0 :(得分:1)

您需要使用html()方法,因为text()会对提供的值进行编码。试试这个:

<script type="text/javascript">
    jQuery(document).ready(function($) {
        var $d = $('.bbp-template-notice p');
        $d.html(function(i, html) {
            return html.trim().replace(/You must be logged in to create new topics./i, 'You must be logged in to create new topics. Please  <a href="http://foo.com/bar.html"><font style="font-weight: bold;">LOGIN HERE</font></a>');
        });
    });
</script>

Working example