【问题标题】:Change line-height property relative to old line-height value相对于旧 line-height 值更改 line-height 属性
【发布时间】:2017-05-26 17:58:25
【问题描述】:

是否有任何解决方案可以相对于 line-height 的旧值更改 line-height 并减少或增加它?

(我的目标是增加或减少 android 中 webView 的行高,但我知道我可以使用一些 css 或 javascript 来做到这一点,所以问题标签是 javascript 和 css。)

【问题讨论】:

  • 例如 line-height 设置为 12 px,我想将其增加 10 乘以 10(22 , 32, 42 , ...)
  • @mohammadhoseinabedini 我已经在下面回答了。
  • @torazaburo 只有在没有为 line-height 指定单位时才适用。可以指定长度单位。
  • @torazaburo 那么,你为什么说这做不到呢?

标签: javascript css space


【解决方案1】:

嗯,是的。

// Get a reference to the element to be changed
var theP = document.querySelector("p");

// Set up a click event handler for the button
document.querySelector("button").addEventListener("click", function(){

  // Get the numeric portion of the current line-height property
  var h = parseInt(window.getComputedStyle(theP, null).lineHeight);
  
  // Set the new value to twice the old value and add the unit back on to the string
  theP.style.lineHeight = (h * 2) + "px";
});
p { line-height:10px; }
<button>Double the line height</button>
<p>The quick brown fox jumped over the lazy dog.<br>But, when he did, the dog bit his tail</p>

【讨论】:

  • 我想在没有任何按钮的情况下执行此操作,因为我的按钮在 android 中,如果你帮我这个忙,我会感谢你(请原谅我,因为我的英语语言不好)@ScottMarcus
  • @mohammadhoseinabedini 那么您希望何时更改line-height?如果您不想要按钮(或某些触发器),只需使用 click 事件处理程序中的代码。
  • 我的 android 活动中有一个函数和一个按钮,当我按下它时,这个函数调用并使用你的 java 脚本代码 @ScottMarcus 更改行高
  • 所以你有一个按钮还是没有?您什么时候“希望”更改line-height?如果不是点击按钮,那么什么时候?
猜你喜欢
  • 2020-05-23
  • 2021-07-14
  • 2012-09-06
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
相关资源
最近更新 更多