22

我正在使用 twitter-bootstrap 和弹出框。

我面临以下问题:当用户单击在页面底部打开弹出窗口的链接时,它首先滚动到页面顶部。这需要用户向下滚动才能看到弹出框打开。在 twitter-bootstrap 站点上的演示中,我没有看到这一点,我想知道是否可以就这个问题获得帮助。

<div class="span1" style=" width: 60px; ">
    <a href="#" class="example2" rel="popover" data-html="true" data-placement="left" data-content="&lt;a href = '/social/2lPdXV1KO4s/Bhangra indian Jingle Bells balle balle Merry Christmas'&gt;&lt;img src='http://i4.ytimg.com/vi/2lPdXV1KO4s/hqdefault.jpg'&gt;&lt;/a&gt;" data-original-title="Bhangra indian Jingle Bells balle balle Merry Christmas">
        <img src="http://graph.facebook.com/1236870349/picture">
    </a>

    <div class="popover fade left in" style="top: 831px; left: 805.61669921875px; display: block;">
    <div class="arrow"></div>
    <div class="popover-inner">
        <h3 class="popover-title">Bhangra indian Jingle Bells balle balle Merry Christmas</h3>
        <div class="popover-content">
            <p><a href="/social/2lPdXV1KO4s/Bhangra indian Jingle Bells balle balle Merry Christmas"><img src="http://i4.ytimg.com/vi/2lPdXV1KO4s/hqdefault.jpg"></a></p>
        </div>
    </div>
</div>
4

5 回答 5

37

看起来您现在必须阻止链接的默认行为,因为他们添加了在悬停/聚焦时弹出弹出框的选项。现在链接将激活,因为 href 指向“#”,它会将您带到页面顶部。以前在旧版本中,它用于自动阻止此默认设置。

您的 javascript 代码应如下所示:

<script>
$("a[rel=popover]")
    .popover()
    .click(function(e) {
        e.preventDefault();
     });
 </script> 

像这样的html

<a href="#" 
   class="btn btn-large btn-danger" 
   rel="popover" 
   data-content="And here's some amazing content. It's very engaging. right?" 
   data-original-title="A Title">
     Click to toggle popover
 </a>

这是一个有效的 JSfiddle。http://jsfiddle.net/hajpoj/KPU47/7/

编辑:或者,您可以使用 div 而不是锚标记,而不必执行整个阻止默认操作。

<div 
   class="btn btn-large btn-danger popover-link"
   data-content="And here's some amazing content. It's very engaging. right?"
   data-original-title="A Title">Click to toggle popover
</div>​

<script>
    $(".popover-link").popover();
</script>
于 2012-12-07T08:54:24.813 回答
12

I have encountered exactly the same issue with the popover and it was enough to replace href="#" with href="javascript://".

于 2014-04-16T18:08:06.450 回答
2

只需使用此代码。没有额外的divclass或任何其他需要。

$("a[data-toggle=popover]").popover().click(function(e) {
        e.preventDefault();
});
于 2017-03-02T16:30:09.857 回答
1

添加 onclick="return false;" 到您的链接;它会阻止浏览器跟随“#”链接:

于 2014-11-06T14:29:20.310 回答
1

与@hajpoj 所说的非常相似,只是顺序很重要。同样,这是一种更好的方法,因为您可以动态地将.popover元素添加到页面,并且它将继续工作。

$('body').on('click', 'a[rel=popover]', function(e) {
  e.preventDefault();
  return;
}).popover({
  selector: 'a[rel=popover]'
});
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac metus nisi. Nulla facilisi. In bibendum ultricies ultrices. Pellentesque sagittis, purus non venenatis mollis, nulla risus fermentum nisi, eget vulputate nulla felis nec mi. Donec rutrum
  nisi sed turpis pulvinar non fermentum risus fringilla. Ut sapien tellus, venenatis sit amet venenatis quis, rhoncus et nulla. Nam augue nisl, consequat vitae convallis sit amet, egestas at ante. Aliquam fringilla viverra porttitor.
</p>
<p>
  Aliquam ornare suscipit posuere. Phasellus ac turpis vitae elit sollicitudin condimentum. Duis massa purus, porttitor sed cursus eu, lacinia at velit. Nam ullamcorper mi ac sapien pretium mollis. Curabitur ante elit, euismod eget elementum condimentum,
  semper a dui. Pellentesque luctus orci elit, ut mattis dolor. Quisque semper nisl eu enim cursus consectetur a in elit. Phasellus faucibus gravida elit vel lobortis. Donec vitae nisi ut libero sollicitudin sodales pretium at purus. Praesent vulputate
  dignissim pulvinar. Nunc sit amet mauris a arcu pulvinar aliquam. Vivamus varius nisl elementum dolor posuere volutpat. Maecenas sed lectus tortor. Aenean sit amet dapibus arcu.
</p>
<p>
  Aliquam posuere sagittis viverra. Aenean velit lorem, vehicula quis viverra vel, porttitor at est. Curabitur at molestie tortor. Quisque at justo eu lacus accumsan aliquam. Ut auctor dui et augue posuere eleifend. Aliquam erat volutpat. Nunc enim enim,
  ullamcorper at scelerisque eu, cursus sit amet lectus. Quisque ac augue erat, vitae tempus diam. Nunc at erat pulvinar lectus vestibulum congue. Vivamus lorem leo, tristique non tempus a, laoreet sit amet ligula. Maecenas tristique vulputate ante, nec
  aliquet enim lobortis vel. Morbi convallis quam sed tellus consectetur hendrerit. Vestibulum vel elit at risus tempor posuere. Vivamus ultricies molestie feugiat. Sed iaculis eros massa. Mauris ac nunc orci.
</p>
<p>
  Aliquam erat volutpat. Pellentesque volutpat iaculis tortor vel hendrerit. Cras sapien orci, fermentum id molestie vel, feugiat in tortor. Quisque vitae felis non tellus volutpat aliquet luctus sit amet felis. Pellentesque gravida tempus turpis, eu lobortis
  eros rutrum eu. In malesuada ultrices leo, eu pretium quam molestie quis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras ac risus eros. Ut posuere lobortis magna, at blandit mauris tristique ut. Suspendisse
  volutpat mollis elit non sagittis. Nam in dictum felis. Maecenas dolor turpis, aliquet vel vestibulum vel, hendrerit non tortor. In pharetra nulla nec arcu rutrum ut commodo tortor facilisis. Curabitur congue feugiat felis vel sollicitudin. Proin in
  mollis dolor. Nulla mattis, turpis vestibulum vehicula blandit, tortor neque posuere diam, sed tempus neque odio id diam.
</p>
<p>
  Vestibulum non quam et elit consequat pellentesque sed at leo. Pellentesque iaculis, purus sit amet cursus imperdiet, orci mi consectetur sapien, sed sodales dui lectus vel enim. Curabitur ac arcu at lectus dictum luctus. Duis et aliquet eros. Suspendisse
  mi mi, porta at elementum pulvinar, pulvinar eu elit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce molestie mollis neque, et elementum odio dapibus sed. Nulla blandit sollicitudin quam semper dictum.
  Pellentesque rutrum rhoncus lacus et facilisis. Sed mattis felis sit amet neque viverra ullamcorper. Fusce volutpat quam scelerisque nisi dignissim vel congue nulla sodales.
</p>


<a href="#" class="btn btn-large btn-danger" rel="popover" data-content="And here's some amazing content. It's very engaging. right?" data-original-title="A Title">
    Click to toggle popover
</a>

于 2014-10-01T21:45:39.297 回答