gpt4 book ai didi

jquery - 在 jQuery 插件中调用函数

转载 作者:行者123 更新时间:2023-12-01 01:54:36 26 4
gpt4 key购买 nike

我正在创建我的第一个 jQuery 插件,它将自动将数字格式化为各种国际格式。插件中有几个函数可以剥离字符串并重新格式化需要从另一个 jQuery 脚本调用的字符串。

根据下面的插件结构(如果您需要完整代码,请告诉我)我可以调用并将参数发送到 stripFormat(ii)targetFormat (ii, iv) 函数?

或者我是否需要更改我的插件结构?如果需要,如何更改?

    (function($){
var p = $.extend({
aNum: '0123456789',
aNeg: '-',
aSep: ',',
aDec: '.',
aInput: '',
cPos: 0
});

$.fn.extend({
AutoFormat: function() {
return this.each(function() {
$(this).keypress(function (e){
code here;
});

$(this).keyup(function (e){
code here;
});

// Would like to call this function from another jQuery script - see below.
function stripFormat(ii){
code here;
}

// Would like to call this function from another jQuery script - see below.
function targetFormat(ii, iv){
code here;
}

});

}
});

})(jQuery);

尝试调用插件函数的方法:

    jQuery(function(){
$("input").change(function (){ //temp function to demonstrate the stripFormat() function.
document.getElementById(targetElementid).value = targetFormat(targetElementid, targetValue);
});
});

我尝试使用这些变体但没有成功:

    document.getElementById(targetElementid).value = $.targetFormat(targetElementid, targetValue);
document.getElementById(targetElementid).value = $.autoFormat().targetFormat(targetElementid, targetValue);

最佳答案

好吧,我可能会更像这个插件:http://www.stilldesigning.com/dotstring/jquery.string.1.0.js

$.string(" <p>this is a test</p> ").strip().stripTags().capitalize().str
> 'This is a test'

或者看一下货币格式,采用值和选项对象。这似乎仅适用于其他格式的情况。 http://plugins.jquery.com/project/currencyFormat

$('#some_element').currency() //will format the text of the element
$.currency(123456.78,{options}) //formats number and returns string

此外,对使用 document.getElementById("targetElement").value 而不仅仅是 $("targetElement").val()$ 感到困惑(“targetElement”).html()

关于jquery - 在 jQuery 插件中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/984202/

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