父 CSS (jQuery)

发布于 2024-08-30 10:46:11 字数 1110 浏览 2 评论 0 原文

这里有

padding-top

<dl id="posters" style="padding-top: 150px;">
<dt class="active">text</dt>
<dd class="active"><a href="#"><img width="150" height="150" src="..." /></a></dd>
<dt>text 2</dt>
<dd><a href="#"><img width="150" height="250" src="..." /></a></dd>
<dt>text 3</dt>
<dd><a href="#"><img width="150" height="350" src="..." /></a></dd>
</dl>

图像之间的区别 - height

尝试编写一个脚本,当单击

时,该脚本将更改
高度

高度应取自 dd imgheight 属性。

尝试过这个,但没有运气:

$("#posters dt").click(function(){
        var parent_padding = $("dd").find("img").height();
        $("dd").closest("dl").css("padding-top",parent_padding);
    }).andSelf().addClass("active")});

谢谢。

There is <dl> with padding-top

<dl id="posters" style="padding-top: 150px;">
<dt class="active">text</dt>
<dd class="active"><a href="#"><img width="150" height="150" src="..." /></a></dd>
<dt>text 2</dt>
<dd><a href="#"><img width="150" height="250" src="..." /></a></dd>
<dt>text 3</dt>
<dd><a href="#"><img width="150" height="350" src="..." /></a></dd>
</dl>

The difference between images - height.

Trying to write a script, which will change a height of the <dl>, when <dd> is clicked.

Height should be taken from the height attribute of the dd img.

Tryed this, but had no luck:

$("#posters dt").click(function(){
        var parent_padding = $("dd").find("img").height();
        $("dd").closest("dl").css("padding-top",parent_padding);
    }).andSelf().addClass("active")});

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光病人 2024-09-06 10:46:16

更新:

$("#posters dd").click(function(){
    var parent_padding = $(this).find("img").height();
    $(this).closest("dl").css("padding-top",parent_padding);
});

Updated:

$("#posters dd").click(function(){
    var parent_padding = $(this).find("img").height();
    $(this).closest("dl").css("padding-top",parent_padding);
});
呢古 2024-09-06 10:46:15

在您的 HTML 中,没有 id 为 posters 的元素,尽管您的选择器正在搜索该元素。可能性很大,但也许这就是问题所在?代码的其余部分看起来完全没有问题。

In your HTML there's no element with the id posters, although that's what your selector is searching. Long shot, but maybe that's the problem? The rest of the code looks perfectly fine.

我要还你自由 2024-09-06 10:46:14

您始终可以使用 jQuery 函数 .attr('height') >.height()

除此之外,它应该适当调整你的 padding-top。

jsbin 预览

Instead of getting the image's .attr('height') you can always just use the jQuery function .height().

Other than that, it should adjust your padding-top appropriately.

jsbin preview

念三年u 2024-09-06 10:46:14

尽管您的语法是正确的,但尝试设置属性也可以这样设置。
.css(属性,值);

示例

$(this).parent().css("padding-top", parent_padding);

$(this).css("color","red");

参考和更多示例:此处

Although your syntax is correct, trying to set a property can be set like this as well.
.css(property, value);

Example

$(this).parent().css("padding-top", parent_padding);

$(this).css("color","red");

Reference and more examples: here

最好是你 2024-09-06 10:46:13

我猜测 a 元素的默认行为是触发并重新加载页面。

父级应该仍然可以工作,因为事件位于

上。

试试这个:

$("#posters dd").click(function(e){
    e.preventDefault();
    var parent_padding = $(this).find("img").attr("height");
    $(this).parent().css({"padding-top":parent_padding});
});

编辑:

看看你的代码,它与你发布的代码有很大不同。

有几个问题。

我认为 end() 没有被正确使用(但不确定)。

prev() 不应接受函数作为参数。

之后就放弃了。

从提供的链接发布代码:

很明显,下面的代码示例不是解决方案。这是您正在使用的实际代码(基于您提供的链接)。我没有采取任何措施来纠正它。我只是指出,如果您发布问题中使用的实际代码而不是修改后的版本,这将非常有帮助。事实证明,您在问题中的修改版本消除了错误。

$("dt").click(function(){
    $(this).siblings()
             .removeClass("active")
             .end()
        .prev(function(){
                    $("dd").parent()
                        .css({'padding-top':$(this).find('img').height()});
    }).andSelf().addClass("active")});
    $("#posters dt").fadeTo("fast",.7);$("#posters dt").hover(function(){$(this).fadeTo("fast",.9)},function(){$(this).fadeTo("fast",.7)});
                                                                         $("#posters .toggle a").click(function(){$(this).toggleClass('active');$("#posters dt").toggle();return false});
  }); 

将来,请发布您实际使用的代码。在这种情况下,您修改了错误。如果您发布了实际的事件处理程序,您就会立即得到解决方案。


部分解决方案:

这是解决方案的开始。

您正在单击事件处理程序内部分配事件。这是不对的,所以我就把它们删除了。我现在将把这些视为一个单独的问题。

这(据我所知)将按照您想要的填充进行操作。请注意,您的点击事件位于 dt 上,而不是 dd 上,因为这就是您在代码中的方式。

如果事件应该在 dd 上,则将其更改为 #('posters dd').click(... 并摆脱 next() 在下一行。

$("#posters dt").click(function(){
    var padding = $(this).next().find('img').attr('height');
    $(this)
        .addClass('active')
        .siblings()
        .removeClass("active");
    $(this).parent()
        .css({'padding-top':padding});
}); ​

I'm guessing that the default behavior of the a element is firing and reloading the page.

Parent should still work, because the event is on the <dd>.

Try this:

$("#posters dd").click(function(e){
    e.preventDefault();
    var parent_padding = $(this).find("img").attr("height");
    $(this).parent().css({"padding-top":parent_padding});
});

EDIT:

Looking at your code, it is quite different from what you posted.

There are several issues.

I don't think end() is being used properly (not sure though).

prev() shouldn't accept a function as an argument.

Gave up after that.

Posted code from link provided:

Just so it is clear, the code example below is not a solution. It is the actual code that you are using (based on the link you provided). I did nothing to correct it. I'm just making the point that it would be extremely helpful if you would post the actual code you're using in your question instead of a modified version. As it turns out, your modified version in the question eliminated the error(s).

$("dt").click(function(){
    $(this).siblings()
             .removeClass("active")
             .end()
        .prev(function(){
                    $("dd").parent()
                        .css({'padding-top':$(this).find('img').height()});
    }).andSelf().addClass("active")});
    $("#posters dt").fadeTo("fast",.7);$("#posters dt").hover(function(){$(this).fadeTo("fast",.9)},function(){$(this).fadeTo("fast",.7)});
                                                                         $("#posters .toggle a").click(function(){$(this).toggleClass('active');$("#posters dt").toggle();return false});
  }); 

In the future, please post the code you're actually using. In this case, you modified out the error. Had you posted the actual event handler, you would have had an immediate solution.


PARTIAL SOLUTION:

This is the beginning of a solution.

You were assigning events inside of the click event handler. This is not right, so I just removed them. I'll consider those to be a separate issue for now.

This (as far as I can tell) will do what you want with the padding. Please notice that your click event is on the dt and not the dd, since that is how you had it in your code.

If the event should be on the dd, then change it to #('posters dd').click(... and get rid of next() on the next line down.

$("#posters dt").click(function(){
    var padding = $(this).next().find('img').attr('height');
    $(this)
        .addClass('active')
        .siblings()
        .removeClass("active");
    $(this).parent()
        .css({'padding-top':padding});
}); ​
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文