jquery - 指定为像素值的行高失败

标签 jquery css height line pixel

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

为什么我无法指定以像素为单位的行高....

这里是 jsfiddle(有效,与我的代码不同):

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

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

编辑:options.css、options.attr 和 options.class 都是对象。我的代码没有错误。删除 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 之后运行

最佳答案

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

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

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

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

关于jquery - 指定为像素值的行高失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19808590/

相关文章:

jquery - 在文档准备好时触发 jQuery 更改函数

jquery - 在jqGrid中动态启用分组

javascript - 使用了 .prop,现在按钮不可点击

javascript - 如何在一组插件选项中添加 jQuery if 语句?

jquery - chrome 不返回正确的 .height()

css - 如何使用 CSS 在同一个外部元素中保持多个图像的相对大小?

CSS 还是 Javascript?

CSS @Font 面部规则不适用于文本框

html - 设置div的高度

jquery - 如何使用 jQuery 获取 div 的全部内容的高度?