gpt4 book ai didi

javascript - 自定义绑定(bind)未按预期工作

转载 作者:行者123 更新时间:2023-12-01 03:00:33 25 4
gpt4 key购买 nike

我有一个 textarea 元素,其样式属性具有此特定属性 ->overflow:hidden;resize:none 和 height:20px。在这种情况下,当我有多行长文本时,我的文本区域将仅显示第一行。我的想法是,我想让它在单击时调整大小,以便整个文本可见。我正在使用 Knockout JS,但我无法意识到我的绑定(bind)出了什么问题。

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&#13;&#10;This line shuld appera only o the second row!</textarea>
</body>
</html>

我还尝试了 element.style.scroolHeight.toString() 以及 data-bind="maximize: active(), minimize: !active()"但它们都不起作用。另外,我 100% 确定我的 bool “active”工作正常(我尝试使用 Chrome 进行调试,它的行为符合我的要求 -> 当我单击文本区域时,它是 true,当我单击外部时,它是 false)。

最佳答案

代码只有三个小问题:

  1. 在您的问题中,您有两个名为 maximize 的绑定(bind)。将其中之一重命名为minimize以避免冲突。

  2. 在绑定(bind)处理程序中,在设置元素的高度之前,您不会验证 valueAccessor 的值。因此,每次 active 可观察到的更改都会触发两个绑定(bind)处理程序。因此最后调用的处理程序将覆盖前一个处理程序的结果。所以你永远不会看到高度变化。

  3. 您的 scroolHeight 中有拼写错误。它应该是 element.scrollHeight

ko.bindingHandlers.maximize = {
update: function(element, valueAccessor) {
if(valueAccessor()) {
element.style.height = element.scrollHeight + "px";
}
}
};

ko.bindingHandlers.minimize = {
update: function(element, valueAccessor) {
if(valueAccessor()) {
element.style.height = "20px";
}
}
};

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.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>
</body>

</html>

更新

当您对多个元素使用单一绑定(bind)时,您必须验证该元素是否是具有焦点的元素。因此,将 element === document.activeElement 添加到 if 条件就足够了。我已经更新了代码片段,对您的代码进行了细微的更改(如评论中发布的),以展示它的工作原理。

ko.bindingHandlers.resize = {
update: function(element, valueAccessor) {
if (valueAccessor() && element === document.activeElement) {
element.style.height = element.scrollHeight + "px";
} else {
element.style.height = "20px";
}
}
};

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.applyBindings(new TextAreaVM());
.txtarea-class {
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},
resize: active()" readonly="true"></textarea>


<br/>
<textarea id="txtarea-id2" class="txtarea-class" height="20px" data-bind="text: text,
event: {focus: setActive, blur: notActive},
resize: active()" readonly="true"></textarea>
</body>

</html>

关于javascript - 自定义绑定(bind)未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46454255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com