<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/knockout-3.4.0.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<a data-bind="attr: { href: url, title: details }">
Report
</a> <script type="text/javascript">
var viewModel = {
url: ko.observable("year-end.html"),
details: ko.observable("Report including final year-end statistics")
};
ko.applyBindings(viewModel)
</script> </body>
</html>
如果属性绑定 data-sonmeth 和css的绑定一样,也需要加入 引号 'data-sonmeth'
下面的会报错
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/knockout-3.4.0.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div data-bind="attr: { data-something: someValue }">...</div> <script type="text/javascript">
var viewModel = {
someValue: ko.observable("{name:'data-something'}"), };
ko.applyBindings(viewModel)
</script> </body>
</html>
下面的时正常的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/knockout-3.4.0.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div data-bind="attr: { 'data-something': someValue }">...</div> <script type="text/javascript">
var viewModel = {
someValue: ko.observable("{name:'data-something'}"), };
ko.applyBindings(viewModel)
</script> </body>
</html>