如何在javascript中删除DIV标记内的CSS类?

时间:2018-03-26 16:36:27

标签: javascript html css html5 css-selectors

请帮助如何删除div标签内的css?

我尝试使用以下代码删除div标签内输入类型的css。但它现在正在工作。

<style>

.removeInputBorder {
    border: none
}
</style>
</head>
<body>

<button onclick="myFunction()">Try it</button>
<div id="myDIV" >
This is a DIV element.
<input type="text" value="shakeer"  class="removeInputBorder"/>
</div>

<script>
function myFunction() {
   var element = document.getElementById("myDIV");
   element.classList.remove("removeInputBorder");
}
</script>

2 个答案:

答案 0 :(得分:4)

您尝试从div中删除removeInputBorder,但该类正在输入。

答案 1 :(得分:0)

供参考,这里是纯javascript中addClass,removeClass,hasClass的解决方案。

查看此页面:http://jaketrent.com/post/addremove-classes-raw-javascript/

&#13;
&#13;
function hasClass(el, className) {
  if (el.classList)
    return el.classList.contains(className)
  else
    return !!el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'))
}

function addClass(el, className) {
  if (el.classList)
    el.classList.add(className)
  else if (!hasClass(el, className)) el.className += " " + className
}

function removeClass(el, className) {
  if (el.classList)
    el.classList.remove(className)
  else if (hasClass(el, className)) {
    var reg = new RegExp('(\\s|^)' + className + '(\\s|$)')
    el.className=el.className.replace(reg, ' ')
  }
}
&#13;
&#13;
&#13;

如何致电:

&#13;
&#13;
var el = document.getElementById('peace')
addClass(el, 'be-still')
// or
removeClass(el, 'be-still')
&#13;
&#13;
&#13;