【问题标题】:jQuery ID attribute change error?jQuery ID 属性更改错误?
【发布时间】:2011-11-21 18:33:59
【问题描述】:

好的,所以我有一个我正在制作的示例网站,而且我主要使用 jQuery。在我的页脚上,我让它在点击时产生动画,并且在点击和动画发生后我让它改变页脚的 ID。但是当我再次单击它(执行新 ID 的 jQuery 命令)时,它不起作用。似乎在更改 ID 后没有执行任何 jQuery 代码。这是使用的代码:

这是在第一个实例中更改 ID 的代码:

$("#footer").click(function(){
    $("#footercontent").animate({height:"200px"});
    $("#footer").attr("id", "footerclose");

这是改回ID的代码:

$("#footerclose").click(function(){
    $("#footercontent").animate({height:"1px"});
    $("#footercontent").hide();
    $("#footerclose").attr("id", "footer");
});

这是页脚内容的 CSS:

#footercontent {
    width:990px;
    height:1px;
    text-align:center;
    background-color:#FFF;
    padding:5px;
    box-shadow:inset 0 0 25px #000;
    border:0px;
}

这是页脚和页脚关闭 CSS:

#footer {
    color:#999;
    font-size: 14px;
    font-family: Arial, Helvetica, sans-serif;
    z-index:99;
    width:150px;
    height:30px;
    border:0px;
    padding:5px;
}

#footerclose {
    color:#999;
    font-size: 14px;
    font-family: Arial, Helvetica, sans-serif;
    width:150px;
    height:30px;
    border:0px;
    padding:5px;

您可以在此处找到相关网站:http://epicgiggle.co.uk/test/example/

我到处找了,没有解决办法。

非常感谢您的帮助。

【问题讨论】:

  • 您是否有理由更改 id 而不是添加/删除类?看来你是在给自己制造不必要的麻烦……
  • 好吧,我想我知道你可以删除一个类/ID,但我不知道你可以改变它们,这可能过于复杂,但我真的不知道在哪里使用类和 ID .真的没有那么大的区别吗?
  • @user1058361 - id 必须是唯一的。
  • 你犯了一个常见的错误:内联语句甚至在前一个语句完成之前就运行了。您应该将 id 更改(如前所述,在整个页面或其中的子页面中必须是唯一的 - 如果您将内容加载到存在新 id 的 div 中,它们不能与现有的 id 冲突)作为回调之后动画: $("#footer").click(function(){ $("#footercontent").animate({height:"200px"},timing, function () { $("#footer").attr ("id", "footerclose") });

标签: jquery html css jquery-animate


【解决方案1】:

您正在尝试将.click() 处理程序绑定到$(document).ready() 中的#footerclose。但是在那个时间点没有具有该 id 的元素,因此处理程序不会绑定到任何东西。

您可以使用.delegate() 以当前方法解决此问题,例如:

$(body).delegate('#footerclose', 'click', function() { ... });

正如@JonathanG 所说,在 jQuery 1.7 中,这应该使用 .on() 代替:

$(body).on('click', '#footerclose', function() { ... })

但老实说,我不会这样做 - 我认为您只需使用 .toggle() (docs) 会更轻松:

$('#footer').toggle(
   function() {
       // animate open, add a class to change the CSS
   },
   function() {
       // close, remove the class
   }
);

【讨论】:

  • 哦,那我可以把#footerclose的jquery代码放在哪里?在 $(document).ready() 部分之后,开始一个新部分?
  • OP 应该使用 jQuery.live 或将事件绑定为 id 更改逻辑的一部分。此外,最好使用类而不是 ID。
  • 重要的是要注意,对于 jQuery 1.7 及更高版本,.live() 和 .delegate() 都已被 .on() 取代。但是,对于 1.7 之前的版本,仍然建议使用 .delegate。
  • 啊,我也可以切换 ID 吗?
  • 您可以切换 id,但我不明白您为什么需要这样做 - 使用类会让您的生活更轻松。大多数 jQuery 开发人员除非需要,否则尽量不要弄乱 id,这是有充分理由的。
【解决方案2】:

您应该使用 jQuery live,因为当您尝试附加 click 事件处理程序时,页面上不存在该元素。

试试这个

$("#footer").live('click', function(){
    $("#footercontent").animate({height:"200px"});
    $("#footer").attr("id", "footerclose");
});

$("#footerclose").live('click', function(){
    $("#footercontent").animate({height:"1px"});
    $("#footercontent").hide();
    $("#footerclose").attr("id", "footer");
});

【讨论】:

    【解决方案3】:

    有点不相关,但是,你可以做一些代码优化。例如,以下每个选择器都做同样的事情 (.hide();):

    $("#homecontent").hide();
    $("#aboutcontent").hide();
    $("#contactcontent").hide();
    $("#othercontent").hide();
    $("#footercontent").hide();
    

    这可以变成:

    $("#homecontent, #aboutcontent, #contactcontent, #othercontent, #footercontent").hide();
    

    此外,.css()(以及.animate();)属性可以组合。所以这个:

    $("#aboutcontent").css({height:"10px"});
    $("#aboutcontent").css({width:"490px"});
    

    可以变成:

    $("#aboutcontent").css({ height:"10px", width: "490px" });
    

    这些不仅限于这些示例。您的 ID 在多个地方执行相同的操作(例如 .hide();),您可以在其中组合选择器。

    最后,我建议使用变量。每次您执行 $("#homecontent") 之类的操作时,jQuery 都会出去并在页面上搜索 ID 为“homecontent”的任何内容。但是,使用变量,例如var homecontent = $("#homecontent"),您可以缓存该选择器,因此不必每次都搜索它。它会运行得更快,并且对您来说更具可读性。如果你这样做了,你的 jQuery 行看起来像这样:

    homecontent.css({ height:"10px", width:"240px" });
    etc etc...
    

    【讨论】:

    • 啊,是的。我以为有人会在这里指出这一点:P 谢谢。 jquery文件中是否使用了var? (可能是一个愚蠢的问题)......是否有指向包含此示例的文件的链接?谢谢你顺便说一句。 :)
    • 是的,您可以将 var 放在 $(document).ready(function(){ 之后。这是一个非常深入地描述变量的链接:msdn.microsoft.com/en-us/site/67defydd
    • 啊,我明白了。我在大学做计算,其中一些代码与我们使用的 Visual Basic.net 类似。有道理,也合乎逻辑。 :P 再次感谢。我的代码可能会更好。 :3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 2014-07-21
    • 2020-06-15
    • 2013-02-24
    • 1970-01-01
    相关资源
    最近更新 更多