指定为像素值的行高失败

时间:2013-11-06 09:43:55

标签: jquery css height line pixels

$(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/

我不知道在我的代码中我做错了什么,但是有一些问题......可能导致这个问题

编辑:options.css,options.attr和options.class都是OBJECTS。我的代码中没有错误。删除options.attr和options.class无效。下面是我在onsole登录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之后运行

1 个答案:

答案 0 :(得分:0)

当使用jQuery的.css()作为setter时,你必须传递一个 Object ,而不是 Array 。所以你的选择必须是这样的:

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

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

简化您的options.css:http://jsfiddle.net/UsEeL/1/