簡體   English   中英

我如何在console.log中點擊一個單擊的bootstrap popover的標題?

[英]How can I console.log the title of a clicked bootstrap popover?

如果我有這個popover代碼:

  $('.UofTennesseeKnox').popover({
    'placement': 'bottom',
    'title': '<a class="passName" href="#">University of Tennessee-Knoxville</a>',
    'content': 'Facilities Services Department 2233 Volunteer Boulevard, Room 203 Knoxville, TN 37996-3010',
    'html': 'true'
  });

獲取.passName內容的代碼不起作用:

        $('.passName').click(function(){
            console.log($(this).html());
        });

難道我做錯了什么? 或者是因為.passName.passName ,我可以做這樣的事情來得到$('.passName').click(function())只在加載彈出窗口后觸發?:

  $('.UofTennesseeKnox').popover({
    'placement': 'bottom',
    'title': '<a class="passName" href="#">University of Tennessee-Knoxville</a>',
    'content': 'Facilities Services Department 2233 Volunteer Boulevard, Room 203 Knoxville, TN 37996-3010',
    'html': 'true'
  }, passNamefunction());

我認為這是因為你用dinamically創建這個元素,嘗試使用.on()

    $(document).on("click", ".passName", function(){
        console.log($(this).html());
    });

您可以使用HTML屬性data-original-title在popover元素上設置數據。 這樣,您以后更容易閱讀數據:

$(".UofTennesseeKnox").attr("data-original-title", "Your title");
$(".UofTennesseeKnox").popover({ 
    // Your other options here
});

var title = $(".UofTennesseeKnox").attr("data-original-title");

我想你甚至可以使用.data()來讀取數據屬性。 我沒有驗證。

var title = $(".UofTennesseeKnox").data("original-title");

暫無
暫無

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

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