【问题标题】:changing the title of a popover with bootstrap and jquery使用引导程序和 jquery 更改弹出窗口的标题
【发布时间】:2012-07-25 18:38:32
【问题描述】:

这是链接的 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>

是的,我正在调用 .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');
}

【问题讨论】:

    标签: jquery twitter-bootstrap popover


    【解决方案1】:

    最简单的方法是这个:

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

    希望对您有所帮助!

    【讨论】:

    • 我一直在寻找的那个小讨厌的 sn-p,感谢 Yann,我的朋友!
    • 非常感谢!我发现它是我自己发现的,因为我注意到没有人在谈论它
    • 为什么 Bootstrap 不为此提供文档或其他一些基本功能?比如popover元素显示时为什么没有API命令获取?
    • 为我工作,设置“数据标题”时它不起作用,所以我很高兴我找到了这个答案
    【解决方案2】:

    看起来 Twitter-bootstrap 没有保留 div 来定位弹出框,因此无法通过更改标题来更改标题。不过,this article 可能会有所帮助。

    Here's a jsfiddle,你可以,呃,摆弄。

    以及为那些无法使用它的人准备的代码。 :-)

    HTML:

    <div style="padding-top: 45px;">
        <a data-content="Original Content for Popover" data-original-title="Popover Title" data-placement="right" href="#" class="btn danger" id="popover-link" rel="popover">Rollover</a>
    </div>​
    

    JavaScript:

    $('#popover-link').popover({title: function() {return 'new title';}});
    $('#popover-link').attr("data-content", "New Content for Popover");
    $('#popover-link').setTitle('new title');​
    

    【讨论】:

    • 那很脏。你知道我喜欢什么。
    • 事实上,这不是解决办法。
    • 太棒了!我只是要修改 JS 并继续前进。谢谢。
    【解决方案3】:

    想想那只猫剥了皮!

    这是交易。似乎在 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
    }
    
    })
    


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

    【讨论】:

    • 弹出框的getTitle方法继承自tooltip构造函数。所以一个getTitle你正在覆盖的方法。
    【解决方案4】:

    这对我有用

    $("#mylink").data()["bs.popover"].config.title = "New Ttile";
    $("#mylink").data()["bs.popover"].config.content = "<p>New Content</p>";
    

    $("#mylink").data("bs.popover").config.title = "New Ttile";
    $("#mylink").data("bs.popover").config.content = "<p>New Content</p>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      相关资源
      最近更新 更多