【发布时间】:2018-03-09 07:38:28
【问题描述】:
我有一个 textarea 元素,它具有样式属性的特定属性 ->overflow:hidden;resize:none 和 height:20px。在这种情况下,当我在多行上有长文本时,我的 textarea 将只显示第一行。我的想法是,当我点击它时,我想让它调整大小,以使整个文本可见。我正在使用 Knockout JS,但我无法意识到我的绑定有什么问题。
function TextAreaVM() {
this.active = ko.observable(false);
this.text = ko.observable("Something\nnew line\nanother new line");
this.setActive = function(){
this.active(true);
};
this.notActive = function(){
this.active(false);
};
ko.bindingHandlers.maximize ={
update: function(element,valueAccessor){
element.style.height = element.style.scroolHeight + "px";
}
};
ko.bindingHandlers.minimize={
update: function(element,valueAccessor){
element.style.height = "20px";
}
};
}
ko.applyBindings(new TextAreaVM());
#txtarea-id{
resize: none;
overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<textarea id="txtarea-id" class="txtarea-class" height="20px"
data-bind="text: text,
event: {focus: setActive, blur: notActive},
maximize: (active() == true),
minimize: (active() == false)"
readonly="true"></textarea>
<textarea id="txtarea-second" class="txtarea-class" height="20px"
width="50px" readonly="true">This is the first line and it should appear only on the first row This line shuld appera only o the second row!</textarea>
</body>
</html>
我也尝试过element.style.scroolHeight.toString() 和data-bind="maximize: active(), minimize: !active()" 之类的东西,但它们都不起作用。此外,我 100% 确定我的布尔值“活动”正常工作(我尝试使用 Chrome 进行调试,它的行为符合我的要求->当我点击文本区域时它是真的,当我点击外部时它是假的)。
【问题讨论】:
标签: javascript html knockout.js data-binding