gpt4 book ai didi

javascript - Knockout.js 基本示例 - 是什么导致此数据绑定(bind)失败?

转载 作者:行者123 更新时间:2023-11-30 18:35:49 24 4
gpt4 key购买 nike

在他们的文档页面上尝试最基本的 Knockout.js 示例: http://knockoutjs.com/documentation/observables.html

看起来我已经像文档要求的那样设置了所有内容,并且我在页面上没有收到任何错误,但是跨度应该显示“personName”的绑定(bind) - 即“Bob”。

但是,当我运行该页面时,出现的全部是“The name is”。 Bob 无处可寻。

这是我的代码:

<!DOCTYPE html>

<html>
<head>
<title>Knockout Practice</title>
<script type='text/javascript' src='javascript/jquery-1.6.4.js'></script>
<script type='text/javascript' src='javascript/jquery-tmpl.js'></script>
<script type='text/javascript' src='javascript/knockout-1.2.1.js'></script>

<script type="text/javascript">

var myViewModel = {
personName: 'Bob',
personAge: 123
};

ko.applyBindings(myViewModel);
</script>

</head>

<body>
The name is <span data-bind="text: personName"></span>


</body>

</html>

文档中是否遗漏了什么,或者我遗漏了什么?

最佳答案

您需要更改脚本的加载位置。

You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery’s $ function.

将脚本 block 放在 HTML 文档的末尾确实显示了

The name is Bob

<span data-bind="text: personName">Bob</span>

关于javascript - Knockout.js 基本示例 - 是什么导致此数据绑定(bind)失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8250689/

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