应用 css 样式时,knockout.js 可见性不起作用
我遇到一个问题,当对项目应用 CSS 样式时,knockout.js 2.0 不显示我的项目。它不会使用所应用的样式来更新显示。如果它关闭,则可以工作。
CSS
.success { display:none }
HTML
<div data-bind="visible: site.signUp.success()" class="success">
Thanks for signining up. You will recieve an email from us in the near future.
</div>
JS
app.viewModel.site.signUp.success(true);
I have an issue where knockout.js 2.0 isn't showing my item when a CSS style is applied to it. It won't update the display with the style applied to it. If it is off it works.
CSS
.success { display:none }
HTML
<div data-bind="visible: site.signUp.success()" class="success">
Thanks for signining up. You will recieve an email from us in the near future.
</div>
JS
app.viewModel.site.signUp.success(true);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在Knockout.js应用绑定之前的一段时间内,您可以通过将默认显示样式设置为
none
来阻止初始渲染/闪烁效果。In the period of time before Knockout.js applies bindings, you can prevent the initial rendering/flashing effect by setting the default display style to
none
.我创建了一个小提琴,展示了如何使用 Knockout 中的 css 绑定来执行此操作。
这是 HTML:
http://jsfiddle.net/johnpapa/vwcfT/ 示例仅使用可见绑定,因为您实际上并不需要 css 类来执行此操作。如果 site.signUp.success observable 为 true,则示例中的第二个 DIV 绑定到名为“success”的 css 类。这比第一个更详细,但如果您需要 css 类做的不仅仅是设置可见性,这可能会很有用。
希望这有帮助。
这是 JavaScript:
I created a fiddle that shows how you can use the css binding in Knockout to do this. http://jsfiddle.net/johnpapa/vwcfT/
Here is the HTML:
The first DIV in the example just uses the visible binding, since you dont really need a css class to do this. The second DIV in the example binds to a css class named "success" if the site.signUp.success observable is true. This is more verbose than the first, but could be useful if you needed your css class to do more than just set visibility.
Hope this helps.
Here is the javascript:
这是因为成功样式定义为
display:none
,相当于visible = false
。您的 CSS 类正在取消您的site.signUp.success()
调用。如果您希望您的 DIV 仅在
site.signUp.success() == true
时显示,只需执行以下操作:That's because the success style is defined as
display:none
, which is equivalent tovisible = false
. Your CSS class is cancelling out yoursite.signUp.success()
call.If you want your DIV to show up only when
site.signUp.success() == true
, just do this:可能有点晚了,但我发现以下内容很有用。无需使用可见性控件来修复每个元素,只需将 div 包裹在所有预隐藏元素周围即可,如下所示:
整个元素部分最初是隐藏的,并且仅在 KO 应用绑定后才显示。我通常用它包裹整个页面以避免任何闪烁问题。
It might be a bit late but I found the following useful. Instead of fixing every element with a visibility control, just wrap a div around all your pre-hidden elements as follow:
The whole section of elements is hidden initially and is only shown after KO has applied bindings. I usually wrap the whole page with it to avoid any flashing problem.
我自己就遇到了这个;我可以理解为什么这样做,但是在页面上后期加载的元素上设置默认可见性为“无”是很方便的,这样它们在加载脚本时就不会闪烁。我能找到的最好的方法就是创建一个简单的自定义绑定:
设置样式时必须小心一点;如果您使用的是
div
并且 CSS 将其设置为display:inline-block
那么此代码将不起作用 - 当inherit< 时它将具有块显示/code> 已应用。然而,这个简单的解决方案非常适合我的用例。
Just run into this myself; I can see why the did it this way, but it is handy to set a default visibility of none on late loaded elements on the page so they don't flash as scripts are loaded. The nicest way I could find of doing this was just to create a simple custom binding:
You have to be a little bit careful when setting up your styles; if you are using a
div
and your CSS sets it todisplay:inline-block
then this code will not work - it will have block display when theinherit
is applied. This simple solution was fine for my use case, however.