【问题标题】:scoping html data with js or jquery使用 js 或 jquery 限定 html 数据
【发布时间】:2017-12-23 11:35:04
【问题描述】:

这是我的 html(在树枝模板中)

<li id="{{folder.id}}" data-jstree='{"icon":"glyphicon glyphicon-tags", "type":"folder"}' ><a href="#">{{folder.name}}</a>

我正在尝试从 'data-jstree' 中获取 'type' 的值。

我尝试过使用

var node_id = ref.get_node(sel[i]).id;
var type = $("#"+node_id).attr("data-jstree");

但这给了我这个:{"icon":"glyphicon glyphicon-tag", "type":"tag"} 我只需要类型的值。

提前致谢。

【问题讨论】:

标签: javascript jquery html attributes


【解决方案1】:
var type = JSON.parse($("#"+node_id).attr("data-jstree")).type

【讨论】:

    【解决方案2】:

    您需要将字符串解析为 json。做这样的事情:

    var node_id = ref.get_node(sel[i]).id;
    var type = $("#"+node_id).attr("data-jstree");
    type = JSON.parse(type).type;
    

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 2012-02-18
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      相关资源
      最近更新 更多