2017-08-27 21 views
0

我正在一個項目上,我從數據庫池圖像src,並保持它隱藏,直到相對於圖像的位置點擊按鈕。但是,圖像是絕對定位的。以下是我的代碼段。我無法定位與jquery唯一定位元素

HTML

<div class="table-responsive"> 
    <table class="table table-bordered table-hover table-striped"> 
     <thead> 
     <tr> 
      <th>S/N</th> 
      <th>Name</th> 
      <th>Phone</th> 
      <th>Proof of Payment</th> 
      <th>Activate User</th> 
     </tr> 
    </thead> 
    <tbody> 

     <tr> 
      <td>1</td> 
      <td>Okolo Michael</td> 
      <td>08062970094</td> 
      <td><button class="btn btn-warning view_pop">View POP</button></td> 
      <td><button class="btn btn-primary">Activate User</button></td> 
      <div class="pop_view"><img src="pop/59a178206fade2.43644948.jpg" alt="File not Found"> <span class="close">X</span></div> 
      </tr><tr> 
      <td>2</td> 
      <td>Okeke Chidimma</td> 
      <td>08044323123</td> 
      <td><button class="btn btn-warning view_pop">View POP</button></td> 
      <td><button class="btn btn-primary">Activate User</button></td> 
      <div class="pop_view"><img src="pop/59a178206fade2.43644948.jpg" alt="File not Found"> <span class="close">X</span></div> 
      </tr><tr> 
      <td>3</td> 
      <td>Anibueze Chigozie</td> 
      <td>08162657108</td> 
      <td><button class="btn btn-warning view_pop">View POP</button></td> 
      <td><button class="btn btn-primary">Activate User</button></td> 
      <div class="pop_view"><img src="pop/59a178206fade2.43644948.jpg" alt="File not Found"> <span class="close">X</span></div> 
      </tr> </tbody> 
    </table> 
</div> 

JQuery的

$('.view_pop').click(function() { 
    $(this).parent().siblings('.pop_view').css('display', 'block'); 
    $(this).html('Viewed'); 
}); 

$('.close').click(function() { 
    $(this).parent('.pop_view').css('display', 'none'); 
}); 

CSS

.pop_view{ 
    display: none; 
    position: absolute; 
    z-index: 5; 
    top: 25%; 
    left: 25%; 

} 

.close{ 
    position: relative; 
    top: 0; 
    right: 0; 
} 

當單擊該按鈕時,圖像不顯示。如何根據點擊的按鈕來選擇這些圖像?

+2

一個DIV不是TR的有效孩子,所以瀏覽器移動它,它不再是兄弟姐妹,並且代碼失敗 – adeneo

+0

@MichaelOkolo did y你檢查了答案嗎? –

回答

1

其實一個<div><tr>是無效的結構,以及瀏覽器中呈現它的頂部(全表本身之外的),這就是爲什麼你的代碼失敗(因爲它沒有更多的tr/tdchild/siblings)。

解決方案: - 轉換<div><td>

工作例如: -

$('.view_pop').click(function() { 
 
    $(this).parent().siblings('.pop_view').css('display', 'block'); 
 
    $(this).html('Viewed'); 
 
}); 
 

 
$('.close').click(function() { 
 
    $(this).parent('.pop_view').css('display', 'none'); 
 
});
.pop_view{ 
 
display:none; 
 
float:left; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="table-responsive"> 
 
<table class="table table-bordered table-hover table-striped"> 
 
<thead> 
 
    <tr> 
 
     <th>S/N</th> 
 
     <th>Name</th> 
 
     <th>Phone</th> 
 
     <th>Proof of Payment</th> 
 
     <th>Activate User</th> 
 
    </tr> 
 
</thead> 
 
<tbody> 
 

 
    <tr> 
 
     <td>1</td> 
 
     <td>Okolo Michael</td> 
 
     <td>08062970094</td> 
 
     <td><button class="btn btn-warning view_pop">View POP</button></td> 
 
     <td><button class="btn btn-primary">Activate User</button></td> 
 
     <td class="pop_view"><img src="pop/59a178206fade2.43644948.jpg" alt="File not Found"> <span class="close">X</span></td> 
 
    </tr><tr> 
 
     <td>2</td> 
 
     <td>Okeke Chidimma</td> 
 
     <td>08044323123</td> 
 
     <td><button class="btn btn-warning view_pop">View POP</button></td> 
 
     <td><button class="btn btn-primary">Activate User</button></td> 
 
     <td class="pop_view"><img src="pop/59a178206fade2.43644948.jpg" alt="File not Found"> <span class="close">X</span></td> 
 
    </tr><tr> 
 
     <td>3</td> 
 
     <td>Anibueze Chigozie</td> 
 
     <td>08162657108</td> 
 
     <td><button class="btn btn-warning view_pop">View POP</button></td> 
 
     <td><button class="btn btn-primary">Activate User</button></td> 
 
    <td class="pop_view"><img src="pop/59a178206fade2.43644948.jpg" alt="File not Found"> <span class="close">X</span></td> 
 
    </tr> </tbody> 
 
</table> 
 
</div>

+0

幹得好,我從你身上學到了一件新東西。+ 1 – Ehsan

+0

@ehsan你好。 –

+1

非常感謝你......這幾乎解決了這個問題。我從中學到了很多東西。我雖然無法在這裏出現,但我對此表示贊同,因爲我對這個社區很陌生,但我感激不盡。謝謝@AlivetoDie –