Skip to content

Instantly share code, notes, and snippets.

@tomoyamkung
Last active December 28, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomoyamkung/7559648 to your computer and use it in GitHub Desktop.
Save tomoyamkung/7559648 to your computer and use it in GitHub Desktop.
[JavaScript]タグに定義されている class を切り替える。 `isNullOrEmpty` は [[JavaScript]文字列が Null かブランクであるかを問い合わせる。](https://gist.github.com/tomoyamkung/7559383) で取り上げたスニペット。
/**
* タグに定義されている class を切り替える。
*
* to が定義されていない、もしくはブランクの場合は from の class を取り除く。
*
* @param {String} id 切り替える class が定義されているタグの ID 属性
* @param {String} from 切り替え対象の class
* @param {String} to 切り替える class
* @returns {}
*/
function toggleClass(id, from, to) {
if(isNullOrEmpty(to)) {
$(id).removeClass(from);
return;
}
$(id).removeClass(from).addClass(to);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment