0

谁能帮帮我,我不明白错误在哪里。

这是我的 jQuery:

function altezzadotgrey(){
    $('.views-field-title').each(function(){
        var aaltezzadotgrey = $(this).outerHeight();
        if ($(this).outerHeight() > 40) {
            $(this).css("margin-top", "-45px");
        }
    });
}

这是我的 HTML:

<div class="views-row">
    <div class="views-field-title">
        <span class="field-content"><a href="">a</a></span>
    </div>
</div>
<div class="views-row">
    <div class="views-field-title">
        <span class="field-content"><a href="">b</a></span>
    </div>
</div>
<div class="views-row">
    <div class="views-field-title">
        <span class="field-content"><a href="">c</a></span>
    </div>
</div>
<div class="views-row">
    <div class="views-field-title">
        <span class="field-content"><a href="">d</a></span>
    </div>
</div>

我认为可能有问题each()

4

3 回答 3

1

jQuery.css 使用“DOM 样式”属性而不是 CSS 样式,所以你这样:

$(this).css("marginTop", -45);

见这里:http: //jsfiddle.net/4upSX/1/

实际上它也适用于“margin-top”......所以你确定你的'.views-field-title'实际上高于40px吗?

于 2013-01-09T11:29:06.437 回答
0
function altezzadotgrey(){

  $('.field-content').each(function(){

        var h= $(this).css('height');
        if (parseInt(h)  > 40) {
            $(this).css("margin-top","-45px"); 
        }
    });
 }
于 2013-01-09T11:10:51.313 回答
0

你只需要在身体负荷上调用你的函数

$( function() {    
    altezzadotgrey();    
});

function altezzadotgrey(){
    $('.views-field-title').each(function(){
        var aaltezzadotgrey = $(this).outerHeight();
        if ($(this).outerHeight() > 40) {
            $(this).css("margin-top", "-45px");
        }
    });
}

其他一切都是正确的

于 2013-01-09T11:46:50.767 回答