我使用以下脚本将类从一个元素复制到另一个元素:
jQuery("#copyto").attr("class", jQuery("#copyfrom").attr("class"));
#copyfrom
看起来像这样:
<div class="blah active">
我想排除'blah'类,只复制'active'。
这有可能吗?
答案 0 :(得分:2)
选项1(旧学校):
var copyfrom = document.getElementById('copyfrom'),
copyto = document.getElementById('copyto');
var old = " "+copyfrom.className+" ";
copyto.className = old.replace(' blah ','');
// alternatively
copyto.className = copyfrom.className.replace(/(^|\s)blah(\s|$)/,'');
选项2(新学校):
copyto.className = copyfrom.className;
copyto.classList.remove( 'blah' );
选项3(只是jQuery):
$('#copyto').attr('class',$('#copyfrom').attr('class')).removeClass('blah');