用json2html渲染anchor href

时间:2014-12-16 10:17:21

标签: javascript jquery html json2html

我正在尝试使用json2html使用以下转换渲染锚点:

'renderTimeline':[  {
    tag: "a",  
    class: "btn btn-warning btn-circle", 
    style: "float: right;", 
    html: "<i class=\"icon-remove\"></i>",
    "href": function() {
        var myhref = "javascript:delSchedule(" + this + ");";
        return myhref;
    }
}]

意图是删除json对象,它以:

传递给它
$('#sunTimeLine').json2html(sched1.sunday, transforms.renderTimeline, {'events':true});

我在渲染的html上得到以下o / p:

<a class="btn btn-warning btn-circle" style="float: right;" href="javascript:delSchedule([object Object]);"><i class="icon-remove"></i></a>

当我点击链接(按钮)时,我在浏览器控制台中收到一条消息:

SyntaxError: missing ] after element list

请帮我解决这个问题。

4 个答案:

答案 0 :(得分:0)

假设您尝试将点击元素的引用传递给delSchedule函数,则需要更改href定义,如下所示:

"href": function() {
    var myhref = "javascript:delSchedule(this);"; // note 'this' is part of the string, not concatenated
    return myhref;
}

答案 1 :(得分:0)

{
  tag: "a",  
  class: "btn btn-warning btn-circle", 
  style: "float: right;", 
  html: "<i class=\"icon-remove\"></i>",
  "href": function() {
      var myhref = "javascript:delSchedule(this);";
      return myhref;
    }
}

答案 2 :(得分:0)

检查documentation以获取更多示例,但您应该使用内置的jquery事件,如此

'renderTimeline':[  {
tag: "a",  
class: "btn btn-warning btn-circle", 
style: "float: right;", 
html: "<i class=\"icon-remove\"></i>",
"onclick": function(e) {
    delSchedule(this);
}}
}]

请注意,json2html只需在事件中添加前缀“on”即可支持大多数jquery事件。例如onclick,onfocus等...

答案 3 :(得分:0)

以下代码解决了我的问题:

'renderTimeline':[  {
    tag: "a",  
    class: "btn btn-warning btn-circle", 
    style: "float: right;", 
    html: "<i class=\"icon-remove\"></i>",
    "onclick": function(e) {
       delSchedule(e);
   }}
}]

如果我传递以下json:

{ monday:[ { startTime:10:00, endTime: 12:00, room_id:cse124 }, { startTime:13:00, endTime: 15:00, room_id:lotus } ] }

我希望能够访问&#34;星期一&#34;在函数delSchedule()中。我该怎么做呢?请帮忙。

相关问题