2016-01-25 38 views
1
<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> 

現在它作爲顯示:如果A文本在B級發現然後用C文本替換它和HTML在替換功能

You must be logged in to create new topics. Please LOGIN HERE"

它正在顯示的HTML代碼,因爲它是在前端。我想讓錨點屬性在該類中工作。

+2

@RoryMcCrossan我們都在同一時間做。所以即使我可以講同樣的事情! ':P' –

回答

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

+0

請在您的答案中刪除我的網站鏈接 – dude88

+0

我已經爲您編輯了它。 –

+0

此答案無效 – dude88