<b id="smsCount"></b> SMS (<b id="smsLength"></b>) Characters left
<br /><textarea id="smsText" style="width:400px;height:200px">
</textarea>
//Plugin
(function($){
jQuery.fn.smsArea = function(options){
var
e = this,
cutStrLength = 0,
s = jQuery.extend({
cut: true,
maxSmsNum: 3,
interval: 400,
counters: {
message: jQuery('#smsCount'),
character: jQuery('#smsLength')
},
lengths: {
ascii: [160, 306, 459],
unicode: [70, 134, 201]
}
}, options);
e.keyup(function(){
clearTimeout(this.timeout);
this.timeout = setTimeout(function(){
var
smsType,
smsLength = 0,
smsCount = -1,
charsLeft = 0,
text = e.val(),
isUnicode = false;
for(var charPos = 0; charPos < text.length; charPos++){
switch(text[charPos]){
case "\n":
case "[":
case "]":
case "\\":
case "^":
case "{":
case "}":
case "|":
case "€":
smsLength += 2;
break;
default:
smsLength += 1;
}
//!isUnicode && text.charCodeAt(charPos) > 127 && text[charPos] != "€" && (isUnicode = true)
if(text.charCodeAt(charPos) > 127 && text[charPos] != "€")
isUnicode = true;
}
if(isUnicode) smsType = s.lengths.unicode;
else smsType = s.lengths.ascii;
for(var sCount = 0; sCount < s.maxSmsNum; sCount++){
cutStrLength = smsType[sCount];
if(smsLength <= smsType[sCount]){
smsCount = sCount + 1;
charsLeft = smsType[sCount] - smsLength;
break
}
}
if(s.cut) e.val(text.substring(0, cutStrLength));
smsCount == -1 && (smsCount = s.maxSmsNum, charsLeft = 0);
s.counters.message.html(smsCount);
s.counters.character.html(charsLeft);
}, s.interval)
}).keyup()
}}(jQuery));
//Start
jQuery(function(){
jQuery('#smsText').smsArea({maxSmsNum:3});
});
我在 Wordpress 工作,M 将制作一些 smscount 系统来计算 ASCII 和 Unicode 字符。我在某处看到 Wordpress 使用 jQuery 而不是 $。但它不起作用。不知道为什么我弄错了?
任何帮助表示赞赏。
改变...
jQuery(function(){
jQuery('#smsText').smsArea({maxSmsNum:3});
});
... 进入...
(function($){
$(document).on('ready', function(){
$('#smsText').smsArea({maxSmsNum:e})
})
})(jQuery);
如果id="smsText"
在 DOM 中存在元素并且脚本定义smsArea()
函数在您运行上面的函数时已准备好/加载,您可以删除document.ready
包装器(删除行2
和4
- 不删除3
!)。
然而,上面语法中的关键不是document.ready
包装器,而是它周围的那个。在“jQuery的包装”。您需要创建一个闭包并将其jQuery
作为引用传递。这是标准的“WordPress jQuery 包装器”,因为在极少数情况下,WP 需要在同一页面上运行不同版本的 jQuery(取决于您加载的插件/主题)。
如果你看一下你上面的函数,它的包装方式是一样的。
这里有更详细的解释。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句