$(element).css(options.css).attr(options.attr).addClass(options.class)
//check if element has changed line-height
if($(element).is('p') && _.isString(options.css['line-height'])){
console.log('line-height assigned: '+ options.css['line-height'] + ' , and now = ' + $(element).css('line-height'))
}
吐出:
line-height assigned: 15px , and now = normal
line-height assigned: 15px , and now = normal
line-height assigned: 15px , and now = normal
line-height assigned: 15px , and now = normal
line-height assigned: 15px , and now = normal
line-height assigned: 33px , and now = normal
为什么我无法以 px 为单位分配行高....
这里的jsfiddle(它有效,与我的代码不同):
http://jsfiddle.net/wyvernmonarch7/S9Scd/
我不知道我的代码做错了什么,但是有一些问题......可能导致这种情况的原因是什么
编辑:选项.css,选项.attr和选项.class都是对象。 我的代码中没有错误。 删除 options.attr 和 options.class 不起作用。以下是我在 Onsole 登录选项时得到的内容.css
Object {line-height: "15px", width: 87, height: 15, font: "11px arial", color: "#FFFFFF"…}
color: "#FFFFFF"
display: "none"
font: "11px arial"
font-weight: 400
height: 15
line-height: "15px"
pointer-events: "none"
text-align: "center"
width: 87
__proto__: Object
line-height assigned: 15px , and now = normal
Object {line-height: "15px", width: 87, height: 15, font: "11px arial", color: "#FFFFFF"…}
color: "#FFFFFF"
display: "none"
font: "11px arial"
font-weight: 400
height: 15
line-height: "15px"
pointer-events: "none"
text-align: "center"
width: 87
__proto__: Object
line-height assigned: 15px , and now = normal
Object {line-height: "15px", width: 87, height: 15, font: "11px arial", color: "#FFFFFF"…}
color: "#FFFFFF"
display: "none"
font: "11px arial"
font-weight: 400
height: 15
line-height: "15px"
pointer-events: "none"
text-align: "center"
width: 87
__proto__: Object
line-height assigned: 15px , and now = normal
///
编辑:在窗口加载后运行
当使用 jQuery 的 .css() 作为 setter 时,你必须传递给它一个对象,而不是一个数组。因此,您的选项必须如下所示:
options = {
css: {
'line-height': '15px'
}
}
参考自jQuery的文档:http://api.jquery.com/css/#css-properties
简化摆弄您的选项.css:http://jsfiddle.net/UsEeL/1/