淘汰模板中的 jquery SlideToggle() 函数

发布于 2024-12-29 03:41:52 字数 422 浏览 0 评论 0原文

因此,如果我想调用带有淘汰赛的 data-bind="click: ShowHide" 的函数,我该怎么做? 请记住,数据绑定位于 li 元素上,该元素由模板填充。我的功能如下:

viewModel = {
LoadedReports: ko.observableArray([]),

ShowHide: function() {

            $(this).children().slideToggle('slow');
        }
}

在我的模板中,我有:

<li data-bind="click: ShowHide, clickBubble: false"><'children elements being populated'></li>

So if I wanted to call a function with knockout's data-bind="click: ShowHide" how would i go about that?
keep in mind the data-bind is on an li element, which is being populated by a template. my function was as follows:

viewModel = {
LoadedReports: ko.observableArray([]),

ShowHide: function() {

            $(this).children().slideToggle('slow');
        }
}

and within my template I have :

<li data-bind="click: ShowHide, clickBubble: false"><'children elements being populated'></li>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看轻我的陪伴 2025-01-05 03:41:52

将 .ShowHide 函数放在表示 li 的对象上(我假设它是一个 LoadedReport 实例。)

将 .ShowHide 放在该对象上。或者,您可以将 .ShowHide 函数放在视图模型上,然后在绑定中使用 click: $root.ShowHide。

Put the .ShowHide function on the object that represents the li (I'm assuming it's a LoadedReport instance.)

Put the .ShowHide on that. Alternately, you can put the .ShowHide function on your view model, then in your binding, use click: $root.ShowHide.

友谊不毕业 2025-01-05 03:41:52

很抱歉,如果有人觉得这个问题已经得到解答,但对我来说答案还不清楚。所以这是一个明显的例子:

<!-- Step 1 - Create the HTML File or the View -->
<!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>

与问题的区别在于我在这个例子中使用

标签而不是

  • Sorry if anyone feels that this is already answered, but to me the answer wasn't clear. So here is a clear example:

    <!-- Step 1 - Create the HTML File or the View -->
    <!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>
    

    The difference from the question is simply that I am using <div> tags instead of <li> for this example.

    还给你自由 2025-01-05 03:41:52

    前几天我自己也得到了这个。请参阅 Judah H. 对我必须做的事情的建议的评论。

    Got this one myself the other day. See the comment on Judah H.'s suggestion for what I had to do.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文