简体   繁体   English

动态div仅在第二次单击后加载

[英]Dynamic div loaded only after second click

I've got an element in the DOM that's draggable and when it's clicked, I want to grab the x/y coordinates (not shown in this example, but for the future) and the height of tooltip that fades in. The source text for the tooltip is an AJAX call and can be of variable length. 我在DOM中有一个可拖动的元素,当单击它时,我想获取x / y坐标(此示例中未显示,但是为了将来)和淡入的工具提示的高度。工具提示是AJAX调用,长度可以可变。 My problem currently is that the shown.bs.tooltip event is only fired on the second click on the triggering element. 我目前的问题是,仅在second单击触发元素时才会触发shown.bs.tooltip事件。 code: 码:

        $('#click').draggable();
        $(document.body).on('click', function () {

            tooltipManager.title();
        });
        $('#click').on('shown.bs.tooltip', function () {
            console.log('from getHeight: ' + getHeight($('.tooltip')));
        });
        var tooltipManager = {
            title: function () {
                //ajax code to get title from database
                $.ajax({
                    type: "POST",
                    contentType: "application/json",
                    url: "Service.asmx/GetDrugs",
                    dataType: "json",
                    success: function (data) {
                        //bootstrap uses the title attribute to set the html inside the tooltip
                        //here it's set to the results of the AJAX
                        var $tooltipData = prettyTooltip(data.d);
                        var offset = $('#click').offset();
                        var windowSize = [
                            width = $(window).width(),
                            height = $(window).height()
                        ]
                        //this fires on the first click
                        console.log(window.width);
                        console.log(offset.top);
                        $('#click').tooltip({
                            trigger: 'click',
                            html: true,
                            placement: tooltipManager.placement.setPlacement(data.d),
                            title: $tooltipData.html()
                            //it seems to me that it would be better design to call the tooltipManager
                            //setPlacement function, but since it's an async request, it fails
                        });
                        //if I add click() at the above line I get an infinite loop of AJAX calls

                    },
                    error: function (xhr) {
                        console.log('failed: ' + xhr.status);
                    }
                });
            },
            placement: {
                left: 'left',
                top: 'top',
                right: 'right',
                bottom: 'bottom',
                //if the value of getHeight is over a certain amount
                //I want to change the position of the tooltip
                setPlacement: function () {
                    var height = getHeight($('.tooltip'));
                    var place = '';
                    if (height < 150) {
                        place = 'right';
                    }
                    else {
                        place = 'left'
                    }
                    return place;
                }
            }
        }
        //not sure if this is good design to have this not a property of the tooltipManager object
        //this works currently for placing the tooltip in the correct position
        function prettyTooltip(data) {
            var $div = $('<div>');
            for (var i = 0; i < data.length; i++) {
                var $p = $('<p>').text(data[i]).appendTo($div);
            }
            return $div;
        }
        function getHeight(el) {
            return $(el).height();
        }

If I use the one method instead of on and I add a click() where the code indicates, the tooltip fires on the first click, but I can't get the offset after the one-time click. 如果我使用one方法代替on并在代码指示的位置添加click(),则工具提示会在第一次点击时触发,但是在一次点击后无法获得偏移量。 How can I make sure I retain all my current functionality and not require two clicks to show the tooltip? 如何确保我保留所有当前功能,而无需单击两次即可显示工具提示?

EDITED: fiddle 编辑: 小提琴

Youre document.body needs to be clicked once to initiate the tooltip. 您只需单击一次document.body即可启动工具提示。 So at the bottom of your document.ready just add a click event. 因此,在document.ready的底部只需添加一个click事件。 Before the first click was initiating the tooltip and then the second click on the actual #click was showing it. 在第一次单击之前启动工具提示,然后在实际的#click进行第二次单击以显示工具提示。

See here: http://jsfiddle.net/RxtBq/2/ 看到这里: http : //jsfiddle.net/RxtBq/2/

I just added 我刚刚添加

$(document.body).click();

right before the end of $(document).ready(function () { 就在$(document).ready(function () {

EDIT: 编辑:

Alternatively. 另外。 You can get rid of the: 您可以摆脱:

$(document.body).on('click', function () {
            tooltipManager.title();
        });

And just call the tooltipManager.title(); 只需调用tooltipManager.title(); at the end of the document.ready function. 在document.ready函数的末尾。

http://jsfiddle.net/RxtBq/3/ http://jsfiddle.net/RxtBq/3/

The most important part is that tooltipManager.title(); 最重要的部分是tooltipManager.title(); is called before you try and click the #click div 在尝试并单击#click div之前被调用

I think the problem is all of the tooltip events are colliding in some way. 我认为问题在于所有工具提示事件都以某种方式发生冲突。 If you need to fetch the tooltip content fresh every time, one way is to destroy and reinitialize. 如果您每次需要重新获取工具提示内容,则一种方法是销毁并重新初始化。

Note that the following code also listens for the additional click to hide the tooltip. 请注意,以下代码还侦听额外的单击以隐藏工具提示。 (If that's not what you wanted, let me know). (如果那不是您想要的,请告诉我)。

if ($('.tooltip:visible').length) { // destroy
  $('#click')
    .tooltip('destroy');
} else { // reinitialize
  $('#click')
    .tooltip('destroy')
    .tooltip({
      trigger: 'click',
      html: true,
      title: $tooltipData.html()})
    .tooltip('show');
}

Updated fiddle: http://jsfiddle.net/RxtBq/4/ . 更新的提琴: http : //jsfiddle.net/RxtBq/4/

you can place a .tooltip('show') after the tooltip is created 您可以在创建工具提示后放置.tooltip('show')

$('#click').tooltip({
                    trigger: 'click',
                    html: true,
                    title: $tooltipData.html()

                }).tooltip('show');

here is the fiddle http://jsfiddle.net/g5mbn/ 这是小提琴http://jsfiddle.net/g5mbn/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM