typeof this.className返回是string,但用replace方法没反應,請各位高手賜教,Thank you!<html>
<head>
<title>Javascript</title>
<style>
.bigSize{width:250px; height:250px}
.smallSize{width:150px; height:150px}
.redColor{background-color:red}
.blueColor{background-color:blue}
</style>
<script type="text/javascript">
window.onload = function(){
var div = document.getElementById("div1");
div.onclick = function(){
if(/bigSize/.test(this.className)){
this.className.replace("bigSize","smallSize");//不能替換字符串
}
else if(/smallSize/.test(this.className)){
this.className.replace("smallSize","bigSize");
}
}
}
</script>
</head>
<body>
<div id="div1" class="bigSize redColor">DIV</div>
</body>
</html>
<head>
<title>Javascript</title>
<style>
.bigSize{width:250px; height:250px}
.smallSize{width:150px; height:150px}
.redColor{background-color:red}
.blueColor{background-color:blue}
</style>
<script type="text/javascript">
window.onload = function(){
var div = document.getElementById("div1");
div.onclick = function(){
if(/bigSize/.test(this.className)){
this.className.replace("bigSize","smallSize");//不能替換字符串
}
else if(/smallSize/.test(this.className)){
this.className.replace("smallSize","bigSize");
}
}
}
</script>
</head>
<body>
<div id="div1" class="bigSize redColor">DIV</div>
</body>
</html>
没有赋值
换句话说 单纯的this.className.replace("smallSize","bigSize");这个操作不会改变this.className如何修改 1楼已经给出了答案