Javascript或Jquery更改类onclick?

fun*_*guy 0 javascript jquery click

我需要改变一个类.author.authornew当我点击.

我的HTML:

<div class="meta-info" id="showhide">
    <div class="author"></div>
 <div id="author-dropdown" style="display: none;"></div>    
Run Code Online (Sandbox Code Playgroud)

我的剧本:

<script>
$(document).ready(function() {
  $('#showhide').click(function() {
    if($('#author-dropdown').css('display') == 'none') {
      $('#author-dropdown').attr("id","author-dropdown-extended").slideDown();
    } else {
      $('#author-dropdown-extended').attr("id","author-dropdown").slideUp();
    }
    return false;
  });
});
</script>
Run Code Online (Sandbox Code Playgroud)

#showhideid按下.#author-dropdown是下拉内容.现在,脚本更改id了下拉内容,但实际上我需要将类更改.author.authornew.我该如何修改脚本来执行此操作?谢谢!

Jul*_*les 6

<script>
   $(document).ready(function() {
       $('div#showhide').click(function() {

           //Check if element with class 'author' exists, if so change it to 'authornew'
           if ($('div.author').length != 0)
                $('div.author').removeClass('author').addClass('authornew');
           //Check if element with class 'authornew' exists, if so change it to 'author'
           else if ($('div.authornew').length != 0)
                $('div.authornew').removeClass('authornew').addClass('author');

       });
   });
</script>
Run Code Online (Sandbox Code Playgroud)

这应该做的伎俩!

首先,它使用类作者删除div 的作者类,然后将类authornew添加到对象中.