【发布时间】:2015-12-06 02:36:55
【问题描述】:
我正在根据字符串出现的次数在某些 wordpress 帖子的底部插入文本。我已经设法通过追加将文本添加到底部,但我想使用 indexOf 在特定位置插入。
这是原文:
if ($('body').hasClass("single-post")){
var count = 0;
var cSearch = $('body').text();
var words = cSearch.indexOf('Words To Find')
while (words !== -1){
count++;
words = cSearch.indexOf('Words To Find', words + 1);
}
if( count >= 2){
$('.entry-content').append('<br><br>Sample Text');
}
}
以下是我将如何获得我想要插入的位置:
var insertLocation = cSearch.indexOf('Show what ya');
如何将“示例文本”拼接到 insertLocation 指定的位置?
我发现了一些关于将 polyfil 用于 .splice 的信息,但我不确定它是否适用。使用它如:
$(cSearch).splice( insertLocation, -1 ).text( "Sample Text" );
有人可以建议一种方法吗?谢谢!
【问题讨论】:
标签: javascript jquery wordpress