我试图在父div之外调用一个类。
$(document).on('click', '.delete_photo', function(event){
var del_id = $(this).attr('id');
$.ajax({
type:'POST',
cache: false,
url:'delete-photo.php',
data:'delete_id='+del_id,
beforeSend:function(){
$(event.target).closest('.photo-over').show();
},
success:function(data) {
$('.editor-photo').load(document.URL + ' .editor-photo-list');
}
});
});这是我的html
<div class="row editor-photo-list">
{foreach from=$row item=$image_file}
<div class="col-md-4" style="margin-bottom: 20px;">
<div class="photo-list">
<input id="{$image_file.id}" type='image' src='http://demo1.demaxglobal.com/content/uploads/editor-images/{$image_file.path}' value='Edit photo' onclick="return launchEditor('{$image_file.id}', 'http://demo1.demaxglobal.com/content/uploads/editor-images/{$image_file.path}');">
<div class="photo-over">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>
</div>
<div class="col-md-12 photo-edit-option">
<div class="col-md-4 photo-btn">
<div id="{$image_file.path}" class="delete_photo"><img src="../content/themes/default/images/close-button.png">
</div>
</div>
<div class="col-md-4 photo-btn">
<input id="{$image_file.id}" type='image' src='../content/themes/default/images/photo-edit.png' value='Edit photo' onclick="return launchEditor('{$image_file.id}', 'http://demo1.demaxglobal.com/conten/uploads/editor-images/{$image_file.path}');">
</div>
<div class="col-md-4 photo-btn">
<div class="post_photo-submit" id="{$image_file.path}">
<img src="../content/themes/default/images/the-flash.png">
</div>
</div>
</div>
</div>
{/foreach}
</div>代码更新。希望这能给你们一些更多的想法。
我想显示div照片-结束点击div删除-照片。我也尝试了jquery ()和find()方法,但是没有帮助。
有多个具有相同类名的div。如果我移除最近的方法,照片覆盖的div将显示在所有的div上。
谢谢
发布于 2017-10-23 19:39:43
我将向这一行标记添加一个函数类,它是每个项的包装器;对应于示例的第3行:
<div class="col-md-4 item-wrapper" style="margin-bottom: 20px;">
然后,您应该可以通过以下方式在回调中访问它:
$(event.target).parents('.item-wrapper').find('.photo-list).find('.photo-over').show()
另外,下一次请在providing code that we can actually run中花费一些精力,将其粘贴到JSFiddle或codepen这样的地方。
发布于 2017-10-23 18:58:45
因为您知道位置,所以您可以使用vanilla js来完成此操作,并使用适当的标记:
Array.from(document.querySelectorAll(".delete_photo")).map(el => {
el.onclick = e => {
const phOver = el.parentElement.previousSibling.querySelector('.photo-over')
// make your Ajax request, then do `$(phOver).show()`
}
})对于jQuery,它应该是:
$(".delete_photo").click(function(e) {
const sec = $(this).parent().prev()
// make your Ajax request, then do `$(".photo-over", sec).show()`
})发布于 2017-10-23 19:42:07
您将需要parent()、prev()和find()方法的组合才能导航到所需的.photo-over元素。
$(event.target).parent().parent().prev().find('.photo-over')导航到前一个.photo-over元素。
这应该适用于你:
$(document).on('click', '.delete_photo', function(event) {
var del_id = $(this).attr('id');
$.ajax({
type: 'POST',
cache: false,
url: 'delete-photo.php',
data: 'delete_id=' + del_id,
beforeSend: function() {
$(event.target).parent().parent().prev().find('.photo-over').show();
},
success: function(data) {
$('.editor-photo').load(document.URL + ' .editor-photo-list');
}
});
});https://stackoverflow.com/questions/46896477
复制相似问题