【问题标题】:How to add click event for button in module of jsontohtml node-js (npm)如何在 jsontohtml node-js (npm) 模块中为按钮添加点击事件
【发布时间】:2020-11-23 19:40:50
【问题描述】:

我正在尝试将 JSON 转换为 HTML。我能够创建 HTML 元素和 ID 属性。但是我无法创建 onclick 事件。我正在使用npm json2htmll.js npm 模块。

let jsontoHtml: {

                "<>": "div",
                "html": [
                     {
                        "<>": "button",
                        "id":"button1",
                        "onclick": "ButtonClicked()",
                        "text": "Click"
                    }
                ]
            };

let html = json2html.transform({}, jsontoHtml);

当前输出:

</button id="button1">click</button>

预期输出:

<button id="button1" onclick="buttonClicked()">Click</button>

你知道我怎样才能达到预期的输出吗?

【问题讨论】:

    标签: npm json2html


    【解决方案1】:

    json2html 不支持使用 npm 模块的事件。相反,我会考虑使用 json2html 的 jquery 插件。您将能够在那里添加将直接挂钩到 jquery 事件的事件。请参阅 json2html.com 上的此示例

    {'<>':'button','onclick':function(e){
        //e.event : the jQuery event
        //e.data  : user specified data from the the json2html options variable eventData (see Usage)
        //e.obj   : the source object that we are transforming
        //e.index : the index of the array in which the source object was found
    }}
    

    【讨论】:

    • 感谢您的回答。但现在我评论了 Json2Html npm 模块中不支持的事件功能。会产生什么问题吗?
    • 当您渲染转换时,您在代码中创建的任何 cmets 都不会显示。
    猜你喜欢
    • 2013-12-08
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多