【问题标题】:How to define a function as data attribute如何将函数定义为数据属性
【发布时间】:2013-10-08 07:34:05
【问题描述】:

在用于填充数据属性的 json 属性对象中包含函数所需的语法是什么?如果我引用该函数,它的 typeof === 'string',但我希望 typeof 产生'function'。这里有两个例子:

<input type="text" data-element='{"setter": setData(id, key, value),"another": "value"}'>

上面的例子不会运行 - javascript 无法解析它。下面的例子产生 typeof 'string'

<input type="text" data-element='{"setter": "setData(id, key, value)", "another": "value"}'

【问题讨论】:

    标签: json custom-data-attribute


    【解决方案1】:

    你不能这样做; JSON 是一种轻量级的数据交换格式,仅支持以下内容:

    • 号码
    • 字符串
    • 布尔值
    • 数组
    • 对象(键:值对的集合,逗号分隔并用大括号括起来)

    http://www.json.org

    http://en.wikipedia.org/wiki/JSON

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 2021-07-04
      • 2018-03-18
      相关资源
      最近更新 更多