簡體   English   中英

使用json2html呈現錨href

[英]rendering anchor href with 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

請幫我解決這個問題。

假設您嘗試將clicked元素的引用傳遞給delSchedule函數,則需要像這樣更改href定義:

"href": function() {
    var myhref = "javascript:delSchedule(this);"; // note 'this' is part of the string, not concatenated
    return myhref;
}
{
  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;
    }
}

查看文檔以獲取更多示例,但是您應該使用內置的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等。

以下代碼解決了我的問題:

'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 } ] }

我希望能夠在函數delSchedule()中訪問“星期一”。 我該怎么做呢? 請幫忙。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM