2013-04-22 34 views
0

我在可通过jquery拖动的页面上动态创建div。z-index javascript css问题

我在每个div上添加了一个小按钮,允许用户通过点击删除div。

我遇到的问题是,我将突出显示的div设置为页面上最重要的项目,但是如果div与另一个div重叠,并且用户单击了删除按钮,则下面的div将被高亮显示,该按钮被点击。

我已经定位了div中的按钮,但位置似乎没有任何影响。该按钮被添加到div。

我会假设它的东西与按钮的z-index的做,但我可能是完全错误的轨道

任何帮助,将不胜感激的。

这里是我用来追加按钮到div的代码。该代码在创建div函数循环内创建div后立即调用。

var but=document.createElement('input'); 
    but.setAttribute('type','button'); 
    but.setAttribute('class','removebutton'); 
    but.style.position = "absolute"; 
    but.style.top = "15px"; 
    but.style.left = +width+"px"; 
    but.style.float="right"; 
    but.style.visibility="hidden"; 

    but.onclick=function(){ 
    if(confirm('Really delete?')) 
     {     
     $.post("delete_box.php",{i:i, page_ref:'<? echo $page_ref; ?>', template_ref:'<? echo $template_ref; ?>'}, function(data,status){ });  
    document.getElementById("frmMain").removeChild(newdiv); 
    document.getElementById(id).removeChild(but); 
    document.getElementById(id).removeChild(newbr); 

     }    
    } 

这里是我使用带来的点击股利前推别的回(每格有dragbox类名)

$('.dragbox').click(function() 
     { 
     $(".removebutton").css('visibility', 'hidden'); 
     $(this).css({ 
     'background-image':'url(img/move.png)', 
     'background-repeat':'no-repeat', 
     'width':'15px', 
     'height':'15px', 
     'zIndex':'1000' 
     }) 
     $(this).find(".removebutton").css('visibility', 'visible'); 

    }); 



$('.dragbox').focusout(function() { 
    $(this).css({ 
        'border', '0px', 
      'background-color', 'transparent', 
      'width', '0px', 
      'z-index', '0' 
     })  
     }); 

回答

1

我认为你需要停止传播的代码按钮上的点击事件。

当用户点击按钮时,点击事件将被传递(又名bubbeling)到点击位置的所有基础元素。

but.onclick = function(e){ 
    e.stopPropagation(); // stop the propagation of the click event to other elements 

    // ... your code    
} 
+1

非常棒,谢谢 – 2013-04-22 09:35:39