jQuery change id! [英] jQuery change id!

查看:126
本文介绍了jQuery change id!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个jQuery noobie,并一直在尝试添加一个脚本来更改div onClick的id。



这里是一个 jsfiddle example

  $ function accept(){
$(div:scrollwrap)。attr('scrollwrap','highlight');
});
pre>

感谢:)

解决方案

attr 应为属性名称,而不是当前值:

  $ accept(){
$(div#scrollwrap)。attr('id','highlight');
});

但是,阅读你的jsFiddle代码,你看起来有一个高亮类,而不是一个ID。 这是我编辑的版本,我认为您正在尝试实现。



请注意,我已经更改了以下内容:




  • 通过添加ID来使.hightlight类更具体

  • 删除了inClick onClick,因为您可以在脚本中执行此操作,这被认为是最佳实践(请参阅JS的 .click()添加)

  • 更改了JS函数来切换类,因为我假设如果用户取消选择复选框, li>


更多资源(jQuery docs):




I am a jQuery noobie, and have been trying to add a script to change the id of a div onClick.

Here is a jsfiddle example.

$(function accept() {
    $("div:scrollwrap").attr('scrollwrap','highlight');
});​

Thanks :)

解决方案

The first parameter of attr should be the attribute name, not the current value:

$(function accept() {
    $("div#scrollwrap").attr('id','highlight');
});​

However, reading your jsFiddle code, you appear to have a class of highlight and not an ID. Here is my edited version with what I think you are trying to achieve.

Note that I have changed the following:

  • Made the .hightlight class more specific by adding the ID, otherwise the highlight style will not override the original.
  • Removed the inline onClick as you can do this within your script, which is considered best practice (see JS for the .click() addition)
  • Changed the JS function to toggle the class, as I assume it should be invalidated if the user deselects the checkbox.

More resources (jQuery docs):

这篇关于jQuery change id!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆