【问题标题】:Configure the Javascript function + Html Helper配置Javascript函数+Html Helper
【发布时间】:2023-12-29 22:38:02
【问题描述】:

有没有办法配置 Javascript(接受少量参数)并使用数据属性以不显眼的方式使用用户指定的参数初始化函数?

任何参考都会有很大帮助

 InitializeLogging({
  Url: "/xxx/yyyy",
  x: 10,
  y: true,
  somevalue: false,
  unHandledErrorCallback: function (message) {
   console.log("Somerror message " + message);
  }
  });

【问题讨论】:

    标签: html-helper unobtrusive-javascript htmlextensions


    【解决方案1】:

    我们可以通过以下方式实现,

    HTML

    <a href="javascript:void(0);" data-attributes="a">Call a</a> <br />
    <a href="javascript:void(0);" data-attributes="b">Call b</a>
    

    脚本

    var app = {
        init: function(){
            this.addEventListener();
        },
    
        a: function(){ alert("You have called 'a'"); },
    
        b: function(){ alert("You have called 'b'"); },
    
        callMethod: function(event){
            var func = $(this).attr('data-attributes');
            app[func].call();
        },
    
        addEventListener: function(){
            $('a').on('click', this.callMethod);
        }
    };
    
    $(document).ready(function(){
        app.init();
    });
    

    演示 JS http://jsfiddle.net/DfHXs/2/

    希望这会对你有所帮助。

    【讨论】:

    • 谢谢..我已经更新了我的问题。正是我正在寻找 html 助手来通过 html 扩展在 View 中初始化上述 JS 函数