这里我粘贴了一个滑动切换子div的代码,但是如果父div包含许多子div,并且每个子div都有唯一的id,那么如何使用knockout js滑动切换id的特定div。
<!DOCTYPE html>
<html>
<head>
<!-- Step 2 - Include jquery and knockout -->
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript" src="http://knockoutjs.com/downloads/knockout-3.0.0.js"></script>
<!-- Step 3 - Add script to run when page loads -->
<script type="text/javascript">
$(document).ready(function(){
<!-- Step 4 - Create a ViewModel -->
function viewModel() {
_self = this;
_self.showHide = function(viewModel, event) {
$(event.currentTarget).children('div').slideToggle();
};
};
<!-- Step 5 - Activates knockout.js bindings -->
ko.applyBindings(new viewModel());
});
</script>
</head>
<body style="">
<div>
Option 1
<!-- Step 6 - Create a HTML Elements with bindings -->
<div data-bind="click: showHide" style="border:2px solid;">
Click me
<div style="display: none;">
Now you see me!
</div>
</div>
</div>
</body>
</html>
只看到这一行并告诉我如何通过id
滑动切换特定的子dib div$(event.currentTarget).children('div').slideToggle(); thanks
答案 0 :(得分:2)
请尽量避免在您的代码中执行此类操作。
正确执行此处所述的操作(fadeVisible绑定):http://knockoutjs.com/examples/animatedTransitions.html
您只需要在节点上添加需要隐藏/显示的绑定。