使用bootstrap和jquery更改popover的标题

cha*_*one 6 jquery popover twitter-bootstrap

这是链接的html,即:

 <a href="javascript:void(0);" style="font-size: 3em; color: #222" class="popover-test" id="directNavPrev" data-title="Previous Result Row" data-content="Previous Result Row">
&laquo;</a>
Run Code Online (Sandbox Code Playgroud)

是的,我正在调用.popover()进行初始化,弹出窗口工作正常.我可以毫不费力地更新内容.只是不是标题.我试过"prev.data('title','new title')",甚至尝试重新初始化"prev.popover({title:'new title'});" 没有骰子......谢谢.

 function SetDirectNavigationPlaceholder() {
    //debugger;
    var item = $("#movementType :selected").val();
    var direct = $('#directNavigation');
    var prev = $('#directNavPrev');
    var next = $('#directNavNext');
    switch (item) {
        case "result":
            $('#bookTypeSection').addClass('hide');
            direct.val('Result Row from 1 - 150');
            prev.attr('data-title', "Previous Result Row");
            next.attr('data-title', "Next Result Row");
            prev.attr('data-content', "Check it! this will contain the information for the previous result<table><tr><td>blah</td><td>blah</td></tr><tr><td>blah</td><td>blah</td></tr><tr><td>blah</td><td>blah</td></tr><tr><td>blah</td><td>blah</td></tr></table>");
            next.attr('data-content', "Check it! this will contain the information for the next result<table><tr><td>blah</td><td>blah</td></tr></table>");
            break;
        case "instrument":
            $('#bookTypeSection').addClass('hide');
            direct.val('Instrument #');
            prev.attr('data-title', "Previous Instrument #");
            next.attr('data-title', "Next Instrument #");
            prev.attr('data-content', "Check it! this will contain the information for the previous <b>instrument</b><table><tr><td>blah</td><td>blah</td></tr></table>");
            next.attr('data-content', "Check it! this will contain the information for the next <b>instrument</b><table><tr><td>blah</td><td>blah</td></tr></table>");
            break;
        case "bookpage":
            $('#bookTypeSection').removeClass('hide');
            direct.val('Book/Page');
            prev.attr('data-title', "Previous Book/Page");
            next.attr('data-title', "Next Book/Page");
            prev.attr('data-content', "Check it! this will contain the information for the previous <b>book/page</b><table><tr><td>blah</td><td>blah</td></tr></table>");
            next.attr('data-content', "Check it! this will contain the information for the next <b>book/page</b><table><tr><td>blah</td><td>blah</td></tr></table>");
            break;
    }
    direct.css('color', '#aaa').not('#directNavigationHeader');
}
Run Code Online (Sandbox Code Playgroud)

Yan*_*bot 19

最简单的方法就是这样:

     var newTitle = "Here's my new title";
     $('#MyExample').attr('data-original-title', newTitle);
Run Code Online (Sandbox Code Playgroud)

希望它有所帮助!

  • 非常感谢 !我发现它是我自己发现的,因为我注意到没有人在谈论它 (2认同)

cha*_*one 1

想想那只被剥了皮的猫!

事情是这样的。看来 bootstrap.js 文件中没有 getTitle() 方法,即使它正在调用。所以我添加了它。享受。

/* NOTE: POPOVER EXTENDS BOOTSTRAP-TOOLTIP.js
 ========================================== */

Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype, {
  constructor: Popover, setContent: function () {
  //debugger;

  var $tip = this.tip()
    , title = this.getTitle() // didn't exist
    , content = this.getContent()

  $tip.find('.popover-title')[this.isHTML(title) ? 'html' : 'text'](title)
  $tip.find('.popover-content > *')[this.isHTML(content) ? 'html' : 'text'](content)

  $tip.removeClass('fade top bottom left right in')
}

, hasContent: function () {
  return this.getTitle() || this.getContent()
}

 , getContent: function () {
  var content
    , $e = this.$element
    , o = this.options

  content = $e.attr('data-content')
    || (typeof o.content == 'function' ? o.content.call($e[0]) : o.content)

  return content
}

 , getTitle: function () { // does now
  var title
    , $t = this.$element
    , n = this.options

  title = $t.attr('data-title')
    || (typeof n.title == 'function' ? n.title.call($t[0]) : n.title)

  return title
}

, tip: function () {
  if (!this.$tip) {
      this.$tip = $(this.options.template)
  }
  return this.$tip
}

})
Run Code Online (Sandbox Code Playgroud)


由于某种原因,我无法获取实际的“标题”属性,因此如果您想更改它,则必须使用“数据标题”。