0
$(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 中指定 line-height ....

jsfiddle here(与我的代码不同,它有效):

http://jsfiddle.net/wyvernmonarch7/S9Scd/

我不知道我的代码做错了什么,但有一些问题......可能是什么原因造成的

编辑:options.css、options.attr 和 options.class 都是 OBJECTS。我的代码没有错误。删除 options.attr 和 options.class 无效。以下是我在单独登录 options.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 

//////////////

编辑:在 window.load 之后运行

4

1 回答 1

0

当使用 jQuery 的 .css() 作为设置器时,您必须将其传递给Object,而不是Array。所以你的选择必须是这样的:

options = {
  css: {
    'line-height': '15px'
  }
}

来自 jQuery 文档的参考:http: //api.jquery.com/css/#css-properties

使用您的 options.css 简化小提琴:http: //jsfiddle.net/UseEeL/1/

于 2013-11-06T10:04:31.557 回答