【发布时间】:2015-06-28 17:36:36
【问题描述】:
我正在向标题字段添加字数显示,类似于编辑帖子时的正文字段(不要问我为什么,这是客户请求)。我已经成功了
- 通过连接到“edit_form_after_title”来显示计数
- 编辑 word-count.min.js 并为我的新计数器元素复制代码
- 编辑 post.min.js 并复制触发器处理程序以更改字数的值(在 post.js 第 960 行附近搜索 // 字数)
既然我已经让它工作了,我的问题是如何让新的 javascript 代码存在于这些文件之外,这样它们就不会在升级时消失?我创建了一个自定义 .js 文件并将其排入我的函数文件中,但我无法让它以相同的方式工作。该文件确实被加载了,但是如果我编写的新 javascript 不在原始文件中,事情就无法正常工作。这可能只是我对 javascript 的有限了解,但我想让这个工作与 wordpress javascript 文件分开。
这是工作 post.js 字数统计 javascript(原始函数第一,我复制的函数第二),它处理重新计算击键时的单词。注意:我还在代码中将“ti”变量设置为较高的位置,其中“co”设置为标题字段。
// Original 'body' word count
if ( typeof(wpWordCount) != 'undefined' ) {
$document.triggerHandler('wpcountwords', [ co.val() ]);
co.keyup( function(e) {
var k = e.keyCode || e.charCode;
if ( k == last )
return true;
if ( 13 == k || 8 == last || 46 == last )
$document.triggerHandler('wpcountwords', [ co.val() ]);
last = k;
return true;
});
}
// Word count for Title
if ( typeof(wpWordCount_title) != 'undefined' ) {
$document.triggerHandler('wpWordCount_title', [ ti.val() ]);
ti.keyup( function(e) {
$document.triggerHandler('wpWordCount_title', [ ti.val() ]);
var k = e.keyCode || e.charCode;
if ( k == last )
return true;
if ( 13 == k || 8 == last || 46 == last )
$document.triggerHandler('wpWordCount_title', [ ti.val() ]);
last = k;
return true;
});
}
这是我的 word-count.js 的重复/调整函数,它处理实际计算单词。
var wpWordCount_title;
! function (a, b) {
wpWordCount_title = {
settings: {
strip: /<[a-zA-Z\/][^<>]*>/g,
clean: /[0-9.(),;:!?%#$¿'"_+=\\/-]+/g,
w: /\S\s+/g,
c: /\S/g
},
block: 0,
wc: function (c, d) {
var e = this,
f = a(".title-word-count"),
g = 0;
d === b && (d = wordCountL10n.type), "w" !== d && "c" !== d && (d = "w"), e.block || (e.block = 1, setTimeout(function () {
c && (c = c.replace(e.settings.strip, " ").replace(/ | /gi, " "), c = c.replace(e.settings.clean, ""), c.replace(e.settings[d], function () {
g++
})), f.html(g.toString()), setTimeout(function () {
e.block = 0
}, 2e3)
}, 1))
}
}, a(document).bind("wpWordCount_title", function (a, b) {
wpWordCount_title.wc(b)
})
}(jQuery);
【问题讨论】:
标签: javascript jquery wordpress admin word-count