我知道Jquery
事件点击,焦点,焦点。如何调用我自己的自定义函数并将这些元素放在函数中,例如
<input type="text" class="pinBox" placeholder="Enter your pin here.">
$('.pinBox').initialize();
一旦执行此操作,就应该有一个函数调用,它必须调用下面的函数。
function initialize(){
var element = $(this);
$(element).after('<input type="password" maxlength="1" autocomplete="off" class="form-control pincode-input-text first" tabindex="30">'+
'<input type="password" maxlength="1" autocomplete="off" class="form-control pincode-input-text second" tabindex="31"> '+
'<input type="password" maxlength="1" autocomplete="off" class="form-control pincode-input-text third" tabindex="32"> '+
'<input type="password" maxlength="1" autocomplete="off" class="form-control pincode-input-text fourth" tabindex="33">); '
}
同样在$('.pinBox').disableInputBox();
上应该有类似的功能。 function disableInputBox(){//whatever i want.}
答案 0 :(得分:0)
select name, value, count(distinct id) as results
from (select 'test' as name, 'test 2' as value, 5 as id)
group by name, value
正如您在此处所见,我已将$.fn.initialize = function() {
// below for example I have given a css property to give you idea how you can refer the element by this to play jquery code with.
this.css( "color", "green" );
return this; // By returning `this`, will allow you to attach further jquery functions after calling this `initialize` function to the element
}
$( ".pinBox" ).initialize().addClass( "my-class" );
函数应用于initialize
,然后又添加了一个jquery函数<a>
。
答案 1 :(得分:0)
您可以使用$ bin/console sylius:install
或$.fn
jQuery.fn
&#13;
function initialize(){
console.log('Element text is :', $(this).text());
}
jQuery.fn.printText = initialize;
$(function(){
$('span').each(function(index, span){
$(span).printText();
})
});
&#13;
您也可以将元素作为参数传递
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<span class='temp'>1</span>
<span class='temp'>2</span>
<span class='temp'>3</span>
<span class='temp'>4</span>
<span class='temp'>5</span>
&#13;
function printText(el){
console.log('Element text is :', $(el).text());
}
$(function(){
$('span').each(function(index, span){
printText(span);
})
});
&#13;
使用哪种?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<span class='temp'>1</span>
<span class='temp'>2</span>
<span class='temp'>3</span>
<span class='temp'>4</span>
<span class='temp'>5</span>
的任何函数都将丢失。像jQuery
这样的图书馆和其他一些图书馆也使用jqLite
并覆盖它。在这种情况下,你将失去你的功能。答案 2 :(得分:-1)
这取决于你希望函数执行的时间......它可以在页面加载,或者在事件上绑定,在下一个代码中举几个例子:
// on page load:
$(function() {
initialize();
});
// on event binding:
$('.classname').on('click', function() {
initialize();
});
注意,如果函数应该对对象执行某些操作,则需要传递它...例如:
$(function() {
initialize( $('.pinBox') );
});
function initialize( elem ){
elem.after( ... );
};
或者如果事件始终在同一个对象上:
$(function() {
initialize();
});
function initialize(){
$('.pinBox').after( ... );
};