1

I need to get jquery-ui tabs functionality working on a div. The div lives inside of a few other elements. It's html bound to a popup marker in a leaflet map.

Here is the html I'm using. I know this html works, as you'll see in the next picture.

Here's a picture of the problem. The html for both the top div and the div in the popup are identical.

Also, my script is called like this:

$(window).load(function() {
    $( ".detail-popup-info" ).tabs();
});

I did that because I wasn't sure if the actual DOM was finished before that got called, but it looks like it didn't really help. I think the issue might have something to do with inheriting lots of other properties? When I inspect the css properties, they look totally different.

The top div i'm guessing doesn't inherit anything since I hard defined it in the html page. The other div, in the popup, is sent in as a string to a function and probably gets shuffled around.

popup error

Here are the css properties of the broken one:

background-color: rgb(255, 255, 255);
cursor: auto;
font-family: "Helvetica Neue",Arial,Helvetica,sans-serif;
font-size: 12px;
font-size-adjust: none;
font-stretch: normal;
font-style: normal;
font-variant: normal;
font-weight: 400;
height: 100px;
line-height: 16.8px;
text-align: left;
width: 200px;
-moz-font-feature-settings: normal;
-moz-font-language-override: normal;

Here are the css properties of the working one:

background-attachment: scroll;
background-clip: border-box;
background-color: rgb(255, 255, 255);
background-image: url("http://localhost:3001/stylesheets/images/ui-bg_flat_75_ffffff_40x100.png");
background-origin: padding-box;
background-position: 50% 50%;
background-repeat: repeat-x;
background-size: auto auto;
border-bottom-color: rgb(170, 170, 170);
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
border-bottom-style: solid;
border-bottom-width: 1px;
border-image-outset: 0 0 0 0;
border-image-repeat: stretch stretch;
border-image-slice: 100% 100% 100% 100%;
border-image-source: none;
border-image-width: 1 1 1 1;
border-left-color: rgb(170, 170, 170);
border-left-style: solid;
border-left-width: 1px;
border-right-color: rgb(170, 170, 170);
border-right-style: solid;
border-right-width: 1px;
border-top-color: rgb(170, 170, 170);
border-top-left-radius: 4px;
border-top-right-radius: 4px;
border-top-style: solid;
border-top-width: 1px;
color: rgb(34, 34, 34);
font-family: Verdana,Arial,sans-serif;
font-size: 9.6px;
height: 100px;
padding-bottom: 1.91667px;
padding-left: 1.91667px;
padding-right: 1.91667px;
padding-top: 1.91667px;
position: relative;
width: 200px;
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;

I guess that's kind of the weird part. The working one has many more css properties than the broken one. Maybe jquery-ui applies some extra properties to it to make it work, but for some reason it isn't applying them to the popup div?

Update

So I guess it turns out jquery actually isn't finding the divs inside the popups because they are dynamically added and removed from the DOM every time one is opened or closed. Here's a picture of the html without the popup opened. Normally, they reside under leaflet-popup-pane, which is empty.

Maybe I'll have to resort to hard-copying the css properties of the working one and applying them manually to the popup divs.

enter image description here

Update 2

Just confirmed it's because the popup html doesn't exist in the DOM. I did a setTimeout on the jquery-ui tabs() function and quickly opened one and it styled it correctly.
But once I close it and reopen it, it's gone again.

4

1 回答 1

1

$( ".detail-popup-info" ).tabs();在触发弹出窗口的同一事件处理程序内 触发选项卡初始化。

这样做load()意味着它检查一次,在页面的资源已经完全加载之后,如果.detail-popup-info还不存在(因为它是在事后通过 AJAX 或用户执行任何打开弹出窗口的操作时添加的)然后它什么都没有初始化。

相反,您希望它在弹出内容添加到 DOM 后进行检查。例如,如果弹出窗口是通过单击按钮创建的,它可能看起来像这样:

$('a.popup-button').on('click', function() {
    // ajax call or whatever that inserts the pop-up content
    $( ".detail-popup-info" ).tabs();
});

如果您正在使用预打包的 jQuery 插件创建弹出窗口,则在创建/初始化弹出窗口后可能还有一个函数回调选项,在这种情况下,您希望将调用.tabs()放在那里。

编辑:我查看了 leaflet.js 文档,您应该可以使用该popupopen事件:

$(document).ready(function() {
    map.on('popupopen', function() {
        $( ".detail-popup-info" ).tabs();
    });
}

我把它放进去,document.ready但你应该把它放在初始化地图的代码之后(他们文档中的示例初始化代码是var map = L.map('map').setView([51.505, -0.09], 13);)。

于 2013-08-01T17:21:22.427 回答