2016-11-07 133 views
2

我將在一個頁面上有多個summernote字段,並且我想要一個動態的方式來獲取元素ID在做onblur時。我似乎無法得到的元素ID,它只是回來不確定獲取Summernote元素的ID

例HTML

<div class="summernote" id="desc_long" name="Long Description">Test 123</div>

的Javascript

$(function() { 
    $('.summernote').summernote({ 
     toolbar: [ 
      ['headline', ['style']], 
      ['style', ['bold', 'italic', 'underline', 'clear']], 
      ['color', ['color']], 
      ['font', ['strikethrough', 'superscript', 'subscript']], 
      ['alignment', ['ul', 'ol', 'paragraph']], 
      ['insert', ['link']], 
     ], 
     onblur: function() { 
      var objid = $(this).attr("id"); 
      alert(objid); 
     } 
    }); 
}); 
+0

我認爲這裏是全局對象 – Geeky

+0

通過event.target訪問id – Geeky

+0

試着在控制檯上打印這個,看看哪個對象變得模糊。的console.log($(本)); –

回答

0

您正在使用哪個summernote的版本?如果它的v0.6.5或更高,則使用駱駝案例,即onBlur而不是onblur。試試這個並檢查。同時發佈你的錯誤,如果有的話。此外,回調函數丟失。檢查http://summernote.org/deep-dive/

+0

感謝您的建議!是舊版本。 –

2

回調 attr爲丟失你的情況

  $('.summernote').summernote({ 
       toolbar: [ 
        ['headline', ['style']], 
        ['style', ['bold', 'italic', 'underline', 'clear']], 
        ['color', ['color']], 
        ['font', ['strikethrough', 'superscript', 'subscript']], 
        ['alignment', ['ul', 'ol', 'paragraph']], 
        ['insert', ['link']], 
       ], 
       callbacks: { 
        onBlur: function(event) { 
        console.log($(this).attr('id')) 
         //alert("onBlur event triggered!"); 
        } 
       } 
      }); 
0

謝謝大家!我使用的是具有非常舊版本Summernote的模板。更新後,Gaurav joshi的代碼是正確的方向。

再次感謝。