【问题标题】:jQuery Change CSS via Class ClickjQuery 通过单击类更改 CSS
【发布时间】:2014-01-14 21:31:22
【问题描述】:

这让我发疯,我尝试了多种方法,但没有任何效果。有没有 jQuery 大神能帮帮我?

jQuery:

$(".FCChatControl").click(function(event) {
    var id = $(this).attr("rel");
    var wnd = $(id);
    if (wnd.style.marginBottom == "-1px") {
        $(wnd).css({"margin-bottom": "-236px"});
    } else {
        $(wnd).css({"margin-bottom": "-1px"});
    }
});

执行自:

<a href="javascript:void(0)" rel="#item.OwnerID#" id="FCChatControlID" class="FCChatControl">
    <div id="cw-header">
        <h1>#item.Nickname#</h1>
    </div>
</a>

【问题讨论】:

  • 控制台显示什么错误?
  • 未捕获的类型错误:无法读取未定义的属性“marginBottom”
  • .style.marginBottom 是纯 JavaScript,您将 jQuery 对象传递给它。
  • 如何检查元素 CSS 是否是 jQuery 中的某个值?我只看到了如何设置它。
  • 阅读手册即可。 api.jquery.com/css $(wnd).css("margin-bottom");// 以px结尾

标签: jquery javascript-events


【解决方案1】:

目前尚不完全清楚您的目标是什么,但现在您需要检查一个 CSS 属性并对其采取行动。

http://jsfiddle.net/isherwood/DxNA3/

$('.FCChatControl').click(function() {
    if ($(this).css('margin-bottom') == "-1px") {
        $(this).css('margin-left', '50px'); // demo
    }
});

顺便说一句,您的else 子句似乎毫无意义,所以我已将其删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多