gpt4 book ai didi

javascript - 如何在 Knockout 中绑定(bind) Html 5 视频控件属性

转载 作者:行者123 更新时间:2023-11-27 22:35:17 25 4
gpt4 key购买 nike

我需要在 Knockout 中有条件地发出 html 5 控件属性。我的意思是有时我会发出控件,有时不会。 attr 绑定(bind)不起作用?有什么东西可以让它发挥作用。

最佳答案

是的,你可以用 attr binding 做到这一点,你只需要从你的 observable 返回 nullundefined 然后 KO 就不会了发出给定的属性:

<video id="movie" width="320" height="240" 
data-bind="attr: {controls: enableControls}">
<source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4" />
</video>
<button data-bind="{click: click}" >Add/Remove Controls </button>

在你的 viewModel 上:

var ViewModel = function()
{
var self = this;
self.enableControls = ko.observable(true);
self.click = function()
{
if (self.enableControls())
self.enableControls(null);
else
self.enableControls(true);
}

}

ko.applyBindings(new ViewModel());

演示 JSFiddle .

关于javascript - 如何在 Knockout 中绑定(bind) Html 5 视频控件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14231314/

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