2016-09-30 91 views
1

我正在翻譯社交網絡,並在帖子中我需要顯示每個建議的投票(類似於在stackoverflow中的答案)的一些統計數據,所以我需要創建彈出窗口,每當用戶懸停在帶有動態內容的標籤上。從功能創建動態彈出窗口

function Popx(id) 
 
{ 
 
    $(id).popover({//here is my problem, I want dynamic id not static 
 
     html: true, 
 
     trigger: 'hover', 
 
     content: function() { 
 
      return $.ajax({url: 'ajax/ajaxpopoverstat.php?uid=1', 
 
       dataType: 'html', 
 
       async: false}).responseText; 
 
     } 
 
    }).hover(function (e) { 
 
     $(this).popover('toggle'); 
 
    }); 
 
}
<div class="label label-default" style="background-color: orange; font-size: x-large" data-poload="ajax/ajaxpopoverstat.php" id="xword" onmouseover="Popx(this.id)">Suggested Word</div>

任何幫助嗎?

回答

0

基於我猜你只是想酥料餅到一個元素反應,你不一定想要的功能,您在您的酥料餅的功能有觸發。如果我是正確的,那麼this JSFIDDLE example should do exactly what you need

$(document).ready(function(){ 
    $('div.label').popover({ //here is my problem, I want dynamic id not static 
     html: true, 
     trigger: 'hover', 
     content: function() { 
     return "Skittles and ids of: " + $(this)[0].id; 
     } 
    }).hover(function(e) { 
     $(this).popover('toggle'); 
    }); 
}); 

此外,在此更新撥弄我讓你酥料餅的正確切換: Updated Fiddle

+0

謝謝,這就是我想要的,它對我很好! :) –

+0

@ParcRoi不客氣 – Blindsyde

1

說明

您選擇具有id變量,而不是本作使用的「#」您的HTML標籤散列jQuery的ID選擇。

代碼

function Popx(id) { 
     // make use of ID selector 
     $('#' + id).popover({ 
     html: true, 
     trigger: 'hover', 
     content: function() { 
      return $.ajax({url: 'ajax/ajaxpopoverstat.php?uid=1', 
       dataType: 'html', 
       async: false}).responseText; 
     } 
     }).hover(function (e) { 
     $(this).popover('toggle'); 
     }); 
    } 
+0

雖然這解決了我最初的問題,酥料餅行事怪異的方式,反正謝謝您善良,這麼多:) –