Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正在尝试更改“边距顶部”属性_Javascript_Jquery_Css - Fatal编程技术网

Javascript 正在尝试更改“边距顶部”属性

Javascript 正在尝试更改“边距顶部”属性,javascript,jquery,css,Javascript,Jquery,Css,这就是我想做的: $(function(){ $('div.item').css('margin-top',-(closest('img').height())/2+'px'); }); 我想要我的“从我所能告诉你的,你正试图做这样的事情: $(function(){ $('div.item').css('margin-top',-($('div.item').closest('img').height())/2+'px'); });

这就是我想做的:

$(function(){           
  $('div.item').css('margin-top',-(closest('img').height())/2+'px'); 
});

我想要我的“从我所能告诉你的,你正试图做这样的事情:

$(function(){           
  $('div.item').css('margin-top',-($('div.item').closest('img').height())/2+'px'); 
});
但是图像选择器取决于您的html配置。
如果您发布一些html标记,也许我可以提供更好的解决方案。

我认为这可能是您需要的,尤其是如果您有多个div.item

编辑

你需要。找到img,然后选择。第一个


示例:

huh-oO这是在FF上工作,但不是在chrome上。也许是因为我已经在div.item?Thx上有了一个监听器,谢谢你们的帮助:-我发现了错误:Math.roundr/2+“px”正在工作。我删除了negactive并替换为top:-50%而不是top:50%似乎Chrome不接受负值我尝试了以下方法:$'div.item'。每个函数{var r=$this.closest'img'。height;$this.css'margin-top',-r/2+'px';};但这在FF和Chrome上不起作用。它需要最接近“img”吗?你想要负利润吗?是的,我不想要负利润。我用位置:绝对;排名前50%;使div中心对齐。除此之外,我尝试了这个,它在FF上工作,但在chrome上没有。chrome不会在控制台中返回任何错误。$'div.item'.eachfunction{var r=$this.find'img'.height;$this.css'margin-top',-r/2+'px';};记住你需要的第一个我试过你的编辑。结果与上一个相同:-它在FF上工作,但在chrome上不工作。我认为这是语法很好,但问题可能来自其他地方。。。但我真的不知道在哪里:
$(function(){           
  $('div.item').css('margin-top',-($('div.item').closest('img').height())/2+'px'); 
});
$('div.item').each(function(){
    var r = $(this).find('img').first().height();
    $(this).css('margin-top', r/2 + 'px'); 
});