如何使jquery插件在用户定义的元素上工作
How to make a jquery plugin work on user defined elements
我正在制作一个基本的jQuery插件,用户可以使用它更改Bg颜色、字体颜色等。
我希望插件的用户能够定义这些更改激活的元素。
我知道我必须使用"这个",但我不知道怎么做。
这是插件的代码
(function($, window, document, undefined){
//Define your own variables first
var wrapper = $('.wrapper');
var p = $('p');
//Define the default settings here
var settings = {
textColor: 'red'
};
//Write your methods here
var methods = {
//Call this method to initialize the plugin
init: function(){
console.log("Initialize the plugin");
$('input').on('change', methods.changeColor);
$('select').on('change', methods.changeFont);
$('.slider').on('change', methods.changeWidth);
},
changeColor: function(){
console.log("This will change the background and/or font color");
var userBackgroundColor = $("#userBackgroundColor").val();
var userTextColor = $("#userTextColor").val().toLocaleLowerCase();
wrapper.css({
backgroundColor: userBackgroundColor,
color: userTextColor
});
},
changeFont: function(){
console.log("This will change the font");
var userFontSize = $("option:selected").val();
console.log(userFontSize);
p.css({
fontSize: userFontSize + 'em'
})
},
changeWidth: function(){
var p = $('p');
var userWidth = $(".slider").val();
var widthFontChange = userWidth / 20;
if (widthFontChange == 1) {
p.css({
width: userWidth + '%',
fontSize: widthFontChange + 'em'
});
}
else {
widthFontChange = userWidth / 2;
p.css({
width: userWidth + '%',
fontSize: widthFontChange + 'px'
})
}
}
};
//Actual plugin call
$.fn.pluginName = function(options){
//If the user overrides defaults by setting his own options
if(options){
settings = $.extend(settings, options);
}
//Put any eventHandlers here, like this:
this.on('change', methods.changeColor);
this.on('change', methods.changeFont);
this.on('change', methods.changeWidth);
//Init the plugin with the $selector
methods.init(this);
//Return this for jQuery chaining
return this;
};
}(jQuery, window, document));
这是一个文件,用户可以在其中定义插件必须在其上工作的对象
$(document).ready(function(){
$('body').pluginName();
});
我的问题是,我如何让它按照我想要的方式工作?
如果我正确理解您的问题,您需要提供设置来选择要侦听更改的各种组件。如果是…
将jQuery this
和options
传递给init
方法,这样它就具备了将事件连接到特定子元素所需的一切。
我转而使用on
的委派版本,因为它可以经受住动态变化。
JSFiddle:http://jsfiddle.net/CgGjq/6/
关键部分是
init: function ($element, options) {
console.log("Initialize the plugin");
$element.on('change', options.input || 'input', methods.changeColor);
$element.on('change', options.select || 'select', methods.changeFont);
$element.on('change', options.slider || '.slider', methods.changeWidth);
},
它使用选项中的属性来覆盖选择器(如果没有提供,则默认为插件默认设置)。
然后,您可以将其与以下选项一起使用:
$(document).ready(function () {
$('body').pluginName({
select: '#userFontSize', // Override the selectors etc
slider: "#userWidth.slider"
});
});
相关文章:
- JavaScript-在手机上不工作的情况下,在外部单击时隐藏元素
- 用javascript将script元素附加到头部;铬不能工作
- 点击功能没有'ajax调用动态元素(Backbone)后无法工作
- 无法使以下ng on click outside指令在同一页面上的两个元素上工作
- DIV怎么能像Javascript中的另一个元素一样工作呢
- javascript()onclick隐藏元素-dos'我不在firefox工作
- 没有获得基础's的Javascript元素工作(与Laravel和Elixir)
- 为什么在ASP.NET Ajax更新面板中执行任何操作后javascript元素都停止工作
- jQuery插件在为元素分配id时停止工作
- 注入的元素和jQuery脚本.如何让他们一起工作
- 自动完成搜索无法正常工作,请不要迭代$中的所有元素.每个ul-li都会及时显示响应
- 让 jquery 为 ajax 添加的元素工作的最佳方法是什么?
- 似乎无法获得'.focus()'元素工作.
- 如何使工具提示从函数本身之外的元素工作
- 似乎不能让JQuery's .on()为通过脚本创建的元素工作
- 添加元素工作,但清除输入值
- jQuery选择器不能为动态创建的元素工作
- IE11 Javascript更改事件不与html表单元素工作
- 欧芹js会为运行时出现的元素工作吗
- 为什么点击一个元素来触发一个事件会阻止另一个元素工作呢?