可能重复:
在javascript中动态添加/删除样式
需要编写 javascript 函数来禁用和启用元素的选择。我写了示例代码:
<style type="text/css">
.unselectable {
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
user-select: none;
}
.selectable {
-moz-user-select: text;
-khtml-user-select: text;
-webkit-user-select: text;
user-select: text;
}
</style>
<script>
function makeUnselectable(node) {
if (node.nodeType == 1) {
node.unselectable = true;
}
var child = node.firstChild;
while (child) {
makeUnselectable(child);
child = child.nextSibling;
}
}
function makeSelectable(node) {
if (node.nodeType == 1) {
node.unselectable = false;
}
var child = node.firstChild;
while (child) {
makeSelectable(child);
child = child.nextSibling;
}
}
</script>
但是这段代码不正确并且不起作用。如何通过javascript动态添加和删除样式组件?所有当前元素的样式和类都应该保留!