mootools_在MooTools 1.2.3中实现.css()和.attr()

mootools

Very rarely does a developer have the ability to work with just one framework. Often a developer needs to float between MooTools and jQuery. As you can imagine, doing so can make a developer loopy due to the syntax difference. One of the small differences that gets used often is jQuery's attr() and css() vs. MooTools' set() and setStyle(). I've implemented css() and attr() into MooTools for jQuery users.

开发人员很少能够只使用一个框架。 开发人员通常需要在MooTools和jQuery之间浮动。 如您所料,由于语法差异,这样做会使开发人员感到困惑。 经常使用的小差异之一是jQuery的attr()和css()与MooTools的set()和setStyle() 。 我已经为jQuery用户在MooTools中实现了css()和attr() 。

MooTools JavaScript代码 (The MooTools JavaScript Code)


/* implement */
Element.implement({
	css: function(property,value) {
		var type = $type(property);
		if(value == undefined && type != 'object') {
			return this.getStyle(property);
		}
		else {
			var pvs = property;
			if(type != 'object') { pvs = {}; pvs[property] = value; }
			return this.setStyles(pvs);
		}
	},
	attr: function(property,value) {
		var type = $type(property);
		if(value == undefined && type != 'object') {
			return this.get(property);
		}
		else {
			var pvs = property;
			if(type != 'object') { pvs = {}; pvs[property] = value; }
			return this.set(pvs);
		}
	}
});

/* css testing! */
//will return "200px"
console.log('css(width) = ' + $('subject').css('width'));
//will set width value to "500px"
console.log('css(width,500) = ' + $('subject').css('width',500));
//will set width value to "700px"
console.log('css(width,700) = ' + $('subject').css({width:700}));

/* attr testing! */
//will return "moo"
console.log('attr(rel) = ' + $('subject').attr('rel'));
//will set width value to "500px"
console.log('attr(rel,mootools) = ' + $('subject').attr('rel','mootools') + ' :: ' + $('subject').attr('rel'));
//will set width value to "700px"
console.log('attr(rel,mootools ftw!) = ' + $('subject').attr({rel:'mootools ftw'}) + ' :: ' + $('subject').attr('rel')


I personally don't prefer this syntax in MooTools but hopefully this helps someone!

我个人不希望在MooTools中使用此语法,但希望对您有所帮助!

翻译自: https://davidwalsh.name/jquery-css-attr-mootools

mootools

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值