简体   繁体   English

我们可以为弹出框内的按钮编写脚本吗?

[英]can we write script for the button which is inside the popover?

I have created a popover so that if I click on the image the popover should appear. 我创建了一个弹出窗口,因此,如果单击图像,则应该显示弹出窗口。

The popover is working. 弹出窗口正在运行。 what my main problem is I have inserted buttons in the popover. 我的主要问题是我在弹出窗口中插入了按钮。

so I want to write javascript or jquery code for the button when it is clicked. 所以我想在单击按钮时为按钮编写JavaScript或jquery代码。 Can anyone help on this? 有人可以帮忙吗?

I have tried but it's not working!!!! 我已经尝试过,但是没有用!!!!

 $(function() { $('button').click(function() { var x = $(this).attr('class'); alert(x); }); }); $(function() { $("[data-toggle=popover]").popover({ html: true, container: 'body', content: function() { var content = $(this).attr("data-popover-content"); return $(content).children(".popover-body").html(); }, title: function() { var title = $(this).attr("data-popover-content"); return $(title).children(".popover-heading").html(); }, placement: "auto" }); }); 
 <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="./isobar.js"> </script> <span> <img src="./img/more_options_icon.png" data-toggle="popover" tabindex="5" data-trigger="focus" data-popover-content="#moreoptions"> </span> <div id="moreoptions" class="hidden"> <div class="popover-body"> <div class="list-group"> <button type="button" class="list-group-item"><span class="gap"></span>Edit</button> <button type="button" class="list-group-item"><span class="gap"></span>Logic Builder</button> <button type="button" class="list-group-item"><span class="gap"></span>Uneploy</button> </div> </div> </div> 

Ok Here is an updated version of my answer and checked and working code. 好的,这是我的答案的更新版本,并检查并工作了。 A secret of a popover is to fire the correspondence function in a right time with a popover firing. 弹出窗口的秘诀是在适当的时候通过弹出窗口触发来启动对应功能。 So the JS code is: 因此,JS代码为:

function firePopover() {
            $('.hidden').css('display', 'block');
            var delay = 100;
            setTimeout(function () {
                $('button:not(.main)').unbind('click');
                $('button:not(.main)').click(function () {
                    var x = $(this).attr('class');
                    alert(x);
                    $('.hidden').css('display', 'none');
                });
            }, delay);
        }

Here I an using html selector 在这里我使用HTML选择器

:not(.main)

to prevent binding and unbinding events to the main button. 防止绑定和取消绑定事件到主按钮。 In addition, we have to pay attention on the fact that every popover rising binds a new event handler to each button. 另外,我们必须注意一个事实,即每次弹出的弹出窗口都会为每个按钮绑定一个新的事件处理程序。 This means that after n popover risings every button will fire it's alert n times. 这意味着在弹出窗口增加n次之后,每个按钮都会触发n次警告。 To prevent this effect, it is possible to bind events in the first rising only, or as I did, to unbind an event from a button every popover rising. 为了防止这种影响,可以仅在第一次上升时绑定事件,或者像我所做的那样,在每次弹出窗口上升时将事件与按钮解除绑定。 As to html code, here it is: 至于html代码,这里是:

<button class="main" onclick="firePopover()">Fire Popover</button>
<div id="moreoptions" class="hidden" hidden>
    <div class="popover-body">
        <div class="list-group">
            <button class="class-0 list-group-item"><span class="gap"></span>Edit</button>
            <button class="class-1 list-group-item"><span class="gap"></span>Logic Builder</button>
            <button class="class-2 list-group-item"><span class="gap"></span>Uneploy</button>
        </div>
    </div>
</div>

I only added the ".main" button to accept a simulation, and each button got additional corresponding class "class-0", "class-1", "class-2" for successful demonstration. 我只添加了“ .main”按钮以接受模拟,并且每个按钮都获得了额外的相应类“ class-0”,“ class-1”,“ class-2”,以进行成功的演示。 Now, when you push on the main button, other 3 buttons appear. 现在,当您按下主按钮时,将出现其他3个按钮。 And to the contrary, pushing on any of those 3 buttons, is following by alert firing and disappearing of all of them. 相反,按下这3个按钮中的任何一个,都会紧接着触发并消失所有这些按钮。 I hope this will help you. 我希望这能帮到您。

  function firePopover() { $('.hidden').css('display', 'block'); var delay = 100; setTimeout(function () { $('button:not(.main)').unbind('click'); $('button:not(.main)').click(function () { var x = $(this).attr('class'); alert(x); $('.hidden').css('display', 'none'); }); }, delay); } 
 .hidden { display: none; } button { float: left; } .class-0 { clear: left; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <button class="main" onclick="firePopover()">Fire Popover</button> <div id="moreoptions" class="hidden" hidden> <div class="popover-body"> <div class="list-group"> <button class="class-0 list-group-item"><span class="gap"></span>Edit</button> <button class="class-1 list-group-item"><span class="gap"></span>Logic Builder</button> <button class="class-2 list-group-item"><span class="gap"></span>Uneploy</button> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用弹出窗口内的按钮关闭bootstrap-popover? - How can i close a bootstrap-popover with a button inside the popover? 启动弹出按钮脚本 - Initiating a popover button script 我如何在提交按钮内使用Bootstrap Popover? - How i can use Bootstrap Popover inside a submit button? 我们可以在一个苗条的组件中编写打字稿吗? - Can we write typescript inside a svelte component? 我们可以在内部的任何div上添加属性(即id / class)吗<script type=“text/template” id=“template”><script> - Can we add attribute(i.e. id/class) on any div which is inside the <script type=“text/template” id=“template”><script> 反应:我们可以将 2 forms 从 2 个不同的子组件传递给父组件,并使用父组件内的按钮提交它们吗? - React: Can we pass 2 forms from 2 different child components to a parent, and submit them with a button which is inside the parent component? 如何在div内的popover中设置选项? - How to make options in popover which is inside div? 试图在弹出框内显示一个按钮元素,但它没有出现 - Trying to display a button element inside of a popover but it is not appearing jQuery单击功能不适用于弹出框内的按钮 - Jquery click function not working for the button inside popover 弹出式窗口未显示在表格行内的按钮中 - Popover not showing in button inside table row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM