gpt4 book ai didi

jquery - 使用 jQuery 动态地将单词包裹在 anchor 元素中

转载 作者:行者123 更新时间:2023-12-01 05:51:14 24 4
gpt4 key购买 nike

我正在尝试编写一个使用 jQuery 将单词动态包装在 anchor 标记中的函数。具体来说,我希望博客文章中作者姓名的每个实例都链接到他们的文章的页面。我现在所拥有的似乎没有表明任何错误,但它不会用链接替换我的名字:

( function( $ ) {
jQuery(document).ready(function ($) {
function linkAuthorRefs( authors, newName ) {
for( i = 0; i < authors.length; i++ ) {
var author = authors[i],
regExAuthor = "/" + author + "/gi",
$elements = $( '.entry-content p:contains( "' + author + '" ), .entry-summary p:contains( "' + author + '" )' );
if( ! newName ) { var newName = author.toLowerCase(); }
for( j = 0; j < $elements.length; j++ ) {
e = $elements[j];
e.html(e.html().replace( regExAuthor, '<a title="View all posts by ' + newName + '" href="http://aaronandrebekah.com/?category=' + newName + '" rel="author">' + author + '</a>' ) );
}
}
}
var authorNames = [ "Aaron", "Rebekah" ];
linkAuthorRefs( authorNames );
linkAuthorRefs( [ "Bekah" ], "rebekah" );
});
} )( jQuery );

最佳答案

感谢您的建议。经过大量研究后,以下内容效果很好!

$.each( [ "Aaron", "Rebekah" ], function( i, j ) {
var author = j,
authorLower = author.toLowerCase(),
regExAuthor = RegExp( "\\b" + author + "\\b", "gi"),
anchoredText = '<a title="View all posts by ' + authorLower + '" href="http://aaronandrebekah.com/?category=' + authorLower + '" rel="author">' + author + '</a>',
$elements = $( '.entry-content p, .entry-summary p' );
$elements.each( function() {
var $par = $( this );
$par.html( $par.html().replace( regExAuthor, anchoredText ) );
});
});

关于jquery - 使用 jQuery 动态地将单词包裹在 anchor 元素中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22033491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com