如何在 Knockout javascript 表中创建文本链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13840376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a text link in a Knockout javascript table?
提问by Pure.Krome
I've got some KnockoutJS code working - it pulls in a list and binds it to a table.
我有一些 KnockoutJS 代码在工作 - 它拉入一个列表并将其绑定到一个表。
For the table-data which displays the name
, I would like that to be an <a href=...>
please, but not sure how. The name is still displayed, btw. But u can click on it.
对于显示 的表数据name
,我希望这是一个<a href=...>
请,但不确定如何。名称仍然显示,顺便说一句。但是你可以点击它。
here's my current code:-
这是我当前的代码:-
<tbody data-bind="foreach: items">
<tr>
<td data-bind="text: name()"></td>
<td data-bind="text: price()"></td>
<td data-bind="text: endsOn()"></td>
</tr>
</tbody>
nothing too crazy.
没什么太疯狂的。
i have another property called url
which contains the full http://blah
url to direct the users to. Also, I would like a new tab to open up, please.
我有另一个名为的属性url
,其中包含http://blah
将用户定向到的完整url。另外,我想打开一个新标签。
Any suggestions?
有什么建议?
回答by Artem Vyshniakov
You have to remove data-bind attribute from td
tag and put a
with attr binding inside td
:
您必须从td
标签中删除 data-bind 属性并a
在里面放置attr 绑定td
:
<tbody data-bind="foreach: items">
<tr>
<td><a data-bind="text: name, attr: {href: url}" target="_new"></a></td>
<td data-bind="text: price"></td>
<td data-bind="text: endsOn"></td>
</tr>
</tbody>
P.S. You don't have to put ()
after property name in data-bind attribute if you don't construct expression.
PS()
如果您不构造表达式,则不必在 data-bind 属性中放置属性名称。