我有一个html页面(在wordpress上),其中有很多带有名字的文章。我想使用jQuery将<article> </ article>
标记内的特定字符串更改为链接。
$('article.single').each(function() {
var text = $(this).text();
$(this).text(text.replace('Name', '<a href="http://xxx.xx/name/">Name</a>'));
});
当我使用此代码时,名称更改正确,但整篇文章显示为纯文本。我应该改变什么?
答案 0 :(得分:2)
...but the entire URL is shown in plain text.
使用.html()而不是.text(),否则会导致html修剪/文本解码(<
获取<
等等...)
$('article.single').each(function() {
var text = $(this).text();
$(this).html(text.replace('Name', '<a href="http://xxx.xx/name/">Name</a>'));
});
答案 1 :(得分:0)
var $thetext = $('#the-text');
var typedText = '';
var replaceText = '<a title="best programming help" href="https://www.stackoverflow.com/">programming</a>';
var programmingRegex = /programming$/gi;
$thetext.on('keyup', function(data){
typedText = $thetext.val();
var matches = typedText.match(programmingRegex);
if(matches){
typedText = typedText.replace(programmingRegex, replaceText);
$thetext.val(typedText);
}
});
答案 2 :(得分:0)
在PHP中执行以避免javascript失败。
$content = str_replace('Name','<a href="http://xxx.xx/name/">Name</a>',the_content());