简体   繁体   English

在IE10中没有立即应用textarea line-height

[英]textarea line-height is not being applied immediately in IE10

I have the following scenario, I have a number of links and whenever one of them clicked font-size and line-height css properties of the textarea set accordingly. 我有以下场景,我有许多链接,每当其中一个点击相应的textarea设置的font-sizeline-height css属性时。

This is an HTML. 这是一个HTML。

<textarea cols="50" id="Target" style="width:400px; height:200px;" rows="20"></textarea>
<br />
<a class="changer" href="javascript:;" data-size="10">10</a><br />
<a class="changer" href="javascript:;" data-size="20">20</a><br />
<a class="changer" href="javascript:;" data-size="30">30</a><br />
<a class="changer" href="javascript:;" data-size="40">40</a><br />

And this is a JavaScript code 这是一个JavaScript代码

$(function(){
    $(".changer").click(function(){
        var size = $(this).data("size");
        $("#Target").css({
            "font-size": size + "px",
            "line-height": (size + 5) + "px"
        });
    });
});

It works perfectly in all browsers except IE10 (didn't tested in IE9, but it is a target too.) The font-size gets applied but line-height takes effect only after the value of the textarea is changed somehow. 它适用于除IE10之外的所有浏览器(在IE9中未经过测试,但它也是目标。)应用font-sizeline-height仅在textarea的值以某种方式更改后生效。

here is the working demo. 是工作演示。

  • Is there any workaround for that? 那有什么解决方法吗? (Programmatically removing and inserting the text back is the least welcomed) (以编程方式删除和插入文本是最不受欢迎的)
  • Is there any approach to make the line-height automatic? 有没有办法让line-height自动?

Since the line-height just updates if you change something, let's give IE something to trigger the update: 因为如果你改变了某些东西就会更新line-height ,让我们给IE一些东西来触发更新:

$(function(){
    $(".changer").click(function(){
        var size = $(this).data("size");
        var text = $("#Target").text(); //grab current text
        $("#Target").css({
            "font-size": size + "px",
            "line-height": (size + 5) + "px"
        }).text(text); //change text with text (same thing, just to trigger the update)
    });
});

Tested on IE9 and Chrome. 在IE9和Chrome上测试过。 Don't have IE10 to test unfortunately but will work I think. 不幸的是,不要让IE10进行测试,但我认为这样可行。

jsFiddle 的jsfiddle

Update: There are alot of ugly things we can do. 更新:我们可以做很多丑陋的事情。 To make IE update, we need to actually make it change it's width, height or text. 要进行IE更新,我们需要实际更改它的宽度,高度或文本。

So you can also do like this one : 所以,你也可以像这一个

$(function(){
    $(".changer").click(function(){
        var size = $(this).data("size");
        $("#Target").css({
            "font-size": size + "px",
            "line-height": (size + 5) + "px",
            "width":$("#Target").width()+1
        }).css({
            "width":$("#Target").width()-1
        });
    });
});

I've just found out a workaround ( fiddle ). 我刚刚发现了一个解决方法( 小提琴 )。 Create a wrapper div where you define the dimensions of your textarea. 创建一个包装器div,您可以在其中定义textarea的尺寸。

<div id="txt_wrapper"><textarea id="Target">This is my text, it spans multiple lines. Line height is not applied immediately</textarea></div>

Then define the size of the textarea in %: 然后在%中定义textarea的大小:

#txt_wrapper {
    width: 400px;
    height: 200px;
}

#txt_wrapper textarea {
    width: 100%;
    height: 100%;
}

Now rotational increase the width to 100.01% then decrease it back to 100%. 现在旋转将宽度增加到100.01%然后将其减小到100%。 Because IE needs this change to get it going(???). 因为IE需要这种改变才能实现(???)。

$(function(){
    var changewidth = 100;
    $(".changer").click(function(){
        var size = $(this).data("size");
        if(changewidth == 100) changewidth = changewidth + 0.01;
        else changewidth = changewidth - 0.01;
        $("#Target").css({
            "font-size": size + "px",
            "line-height": (size + 5) + "px",
            "width": changewidth+"%"
        });
    });
});

I think this is just a bug and I have absolutely no idea why this is working. 我认为这只是一个错误,我完全不知道为什么这样做。 It looks like changes in a very small % range which can not be calculated into a usable pixel value and therefore will not result in a visual change still fire the needed "change event" on the textbox. 看起来像非常小的%范围内的变化无法计算为可用的像素值,因此不会导致视觉变化仍然会激发文本框中所需的“更改事件”。 I've developed this sollution based on an information in the msdn forums . 我根据msdn论坛中的信息开发了这个sollution。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM