19

我更像是一个 C++ 人,但我目前正在研究一些 Web UI 的东西。我似乎无法让 Bootstrap 弹出框出现在 SVG 元素上。

有任何想法吗?弹出框适用于普通 div。

jsFiddle 在这里:http: //jsfiddle.net/JjAht/

<!DOCTYPE html>
<html>
<head>
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/css/bootstrap.min.css" rel="stylesheet">
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/css/bootstrap-responsive.min.css" rel="stylesheet">

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/bootstrap.min.js"></script>
</head>
<body>
<div>
    <svg width="100" height="100">
        <circle r="45" cx="50" cy="50" style="fill: red"/>
    </svg>
</div>

<div class="well">
    <p>
    Hover here for a popover.
    </p>
</div>

<script type="text/javascript">

    $(document).ready(function() {
        var numCircles = $("circle").length;
        console.log("numCircles = " + numCircles);

        $("circle").popover({trigger:'hover', placement:'bottom', title:'Title!', content:'Content'});

        $("circle").css({"stroke":"blue"});

        $(".well").popover({trigger:'hover', placement:'bottom', title:'Title!', content:'Content'});
    } );
</script>
</body>
</html>
4

3 回答 3

25

从该示例看来,您需要修改 Bootstrap 才能使其正常工作。

在 bootstrap-tooltip.js 下替换:

$tip
    .detach()
    .css({ top: 0, left: 0, display: 'block' })
    .insertAfter(this.$element)

pos = this.getPosition(inside)

if($('#'+this.$element[0].id,$('svg')).length > 0){
        $tip
          .remove()
          .css({ top: 0, left: 0, display: 'block' })
          .prependTo(document.body)

        pos = $.extend({}, this.$element.offset(), {
          width: this.$element[0].getBoundingClientRect().width
          , height: this.$element[0].getBoundingClientRect().height
        })
   } else {
         $tip
             .detach()
             .css({ top: 0, left: 0, display: 'block' })
             .insertAfter(this.$element)

         pos = this.getPosition(inside)
    }

基本上有两个问题 1)引导程序将工具提示/弹出框的 div 插入到浏览器忽略它的 SVG 中,2)需要从 SVG 计算位置信息

我已经在 Github https://github.com/twitter/bootstrap/pull/6521上将此作为拉取请求提交

更新:看来 bootstrap 2.3.0+ 将通过container工具提示/弹出框的新属性支持这一点。使用 SVG 时,将其设置containerbody. 在这里查看评论https://github.com/twitter/bootstrap/pull/6521

一个例子:

svg.selectAll(".node").each(
    function(d,i){
        $(this).popover({title:"title", content:"content", container:"body"});
        // and/or $(this).tooltip({title:"tooltip - title", container:"body"});
    });
于 2013-01-09T17:20:36.727 回答
16

使用最新版本的 Bootstrap,container在里面添加一个带有 HTML 元素的选项似乎就足以让它工作了!

$("circle").each(function () {
    $(this).popover({
        title: "Hi!",
        content: "popover",
        container: $("#container")
    });
});
于 2013-05-30T17:33:18.797 回答
-1

您尚未选择 svg 来打开弹出框,这就是它无法打开的原因。看这里http://jsfiddle.net/JjAht/1/

    $("svg").popover({trigger:'hover', placement:'bottom', title:'Title!', content:'Content'});
于 2012-12-24T18:06:40.567 回答