【问题标题】:Can I accomplish what Bootstrap Popover does with a few functions instead of a full plug-in?我可以用几个功能而不是完整的插件来完成 Bootstrap Popover 的功能吗?
【发布时间】:2014-08-05 00:55:43
【问题描述】:

我研究了 Bootstrap Popover 功能的几种替代方案,但没有找到有效的解决方案。当我想要做的只是通过单击图像元素打开一个小的“语音气泡”div 时,Popover 插件似乎有点矫枉过正。如果用户单击触发此事件的元素、语音气泡本身或页面上的任何位置,语音气泡应该会再次关闭。我想在一页上有几个。如果用户打开一个气泡并单击另一个触发元素,则当前打开的气泡将关闭,新的气泡将打开。在可切换方面,它几乎就像来自 jQueryUI 的手风琴。

我有一些代码,但我还没有完全掌握 jQuery。我需要switch 声明还是.each()

HTML:

<div class="testimonial">
    <p class="leftCol">
        Click the link:
        <br />
        <a href="#" class="trigger" id="trig1">I'm the first link</a>
    </p>
    <div class="voiceContainer" id="vc1">
        <div class="voiceContent">
            You clicked the first link.
        </div>
        <div class="arrow arrow-bottom"></div>
    </div>
</div>
<div class="testimonial">
    <p class="leftCol">
        Click the link:
        <br />
        <a href="#" class="trigger" id="trig2">I'm the second link</a>
    </p>
    <div class="voiceContainer" id="vc2">
        <div class="voiceContent">
            You clicked the second link.
        </div>
        <div class="arrow arrow-bottom"></div>
    </div>
</div>

我尝试过的jQuery:

$(".testimonial").each(function(){
    $(this).find(".voiceContainer").hide();
});

$(".testimonial").each(function(){
    $(this).find(".trigger").click(function() {
        // I'm stuck here. I don't think that .voiceContainer is a sibling of .trigger
    });
});

我是在正确的轨道上,还是完全偏离了基地?任何帮助表示赞赏。

【问题讨论】:

  • 是什么让你觉得 Bootstrap 的解决方案有点矫枉过正?
  • 好吧,我没有使用 Bootstrap,一个:p

标签: jquery twitter-bootstrap popover


【解决方案1】:

http://jsfiddle.net/6W253/3/ 不确定这是否是您想要的。有点草率。

CSS

.hide { display:none; }

Javascript:

$(".voiceContainer").each(function () {
    $(this).addClass("hide");

});$(".testimonial").each(function () {
    $(this).find(".trigger").click(function () {

        var voiceContainers = document.getElementsByClassName("voiceContainer");
        var triggers = document.getElementsByClassName("trigger");    
        for ( i = 0; i < triggers.length; i++)
        {   
            voiceContainers[i].className =  voiceContainers[i].className + " " + "hide";

        if (this == triggers[i]) 
        {               
            voiceContainers[i].className = "voiceContainer";                        
        }


        }
    });
});

【讨论】:

  • 不太确定这是做什么的?我查看了演示,但单击链接并没有改变任何内容。
  • @NojoRu 现在试试吧抱歉。
  • 是的,这可以完成工作。如果用户单击另一个“单击我”链接,是否可以添加一项功能,前一个链接将关闭?如果你愿意的话,就像一个切换开关,但是是多维的。
  • 是的,完全正确。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-08
  • 1970-01-01
  • 2020-06-08
  • 2015-02-10
  • 2011-07-07
  • 2014-12-14
相关资源
最近更新 更多