javascript - 有没有办法在读取 html 时修改 json 字符串?

标签 javascript html json parsing knockout.js

所以我的 JSON 中有一个复杂的对象结构,它是一个列表,每个元素都有许多属性。我希望能够访问名称属性并将其整个输出,即:“任务一”,并在加载将引用“链接到任务一”的文本时动态添加 href。

我正在使用knockout js,这就是该行的样子

<a class="this-class" data-bind="text: taskName attr: {href: '#link-to-'+taskName.replace(/\s+/g,'-')}"></a>

我知道 js 函数替换在 html 文档中不起作用,我只是想知道是否有一种方法可以强制 json 呈现在某些标签内,或者是否有人可以帮助我找到一种放入其中的方法这不会花费太多时间。

或者,如果我很愚蠢,应该在我的 JSON 中添加另一个名为“ref”的属性或其他内容

最佳答案

为什么不将计算添加到您的模型中:

self.link = ko.computed(function () {
    return '#link-to-' + self.taskName().replace(/\s+/g,'-');
}, self);

然后:

<a class="this-class" data-bind="text: taskName, attr: { href: link }"></a>

关于javascript - 有没有办法在读取 html 时修改 json 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44924603/

相关文章:

html - 在 CSS 选择器中使用标签名称可以吗?

javascript - 为什么 JSON API 规范建议使用连字符/减号来分隔成员名称中的单词?

arrays - 类型 'String' 不符合协议(protocol) 'NSCopying' - 数组 swift json 错误

php - 从 Ajax 响应中获取多个数据

javascript - 滑动图像缓动

javascript - 选择框 onchange 更新多个项目

jQuery jscrollpane vertical with white-space :nowrap CSS prop

javascript - foreach 单选按钮显示更多信息

javascript - 通过javascript切换html内容的最佳方法

java - jackson 反序列化 : How to map specific properties to getters and setters as well as loading all the properties into map of a same POJO?