【问题标题】:maximize a div & change its fontSize最大化 div 并更改其字体大小
【发布时间】:2016-03-22 10:15:10
【问题描述】:

我的 html 中有 2 个 div,我想通过 onclick 增加大小,但 fontSize 没有改变。

代码:

function changeSize(id, weight, height)
{
with (document)
{
	if (getElementById(id).style.width = '240px'){
		getElementById(id).style.width = weight + 'px';
		getElementById(id).style.height = height + 'px';
		getElementById(id).style.fontSize = '30px';
	}	
	else {
		getElementById(id).style.width ='240px';
		getElementById(id).style.height ='300px';
		getElementById(id).style.fontSize = '18px';

	}
}
}
.kaesten{
	width:240px;
	height:300px;
	background-color:darkgrey;
	background-position:center;
	background-repeat:no-repeat;
	text-shadow:0px 0px 3px #000;
	border: 5px solid #F0F8ff;
	vertical-align:top;
	text-shadow: 3px 3px 4px #777;
	float:left;
	margin-left:30px;
}
<div id="box1" class="kaesten" onclick="maxi('box1', 600, 600); return false;">
test1

</div>

<div id="box2" class="kaesten">
test2
</div>

问题:如何更改字体大小?以及如何通过第二次单击将 div 大小再次更改为原始大小?

【问题讨论】:

  • 试试getElementByID(id).style.fontSize = '30px';
  • 试试 getElementById(id).style.fontSize = '30' +'px';
  • 错误是 ID 而不是 ID

标签: javascript html css font-size maximize


【解决方案1】:

你打错了。 JavaScript 中的属性和方法是区分大小写的。方法是getElementById,而不是getElementByID

假设元素上唯一的内联样式是您在上面的函数中添加的样式,您可以使用以下方法删除样式并“重置”元素:

function changeSize(id, weight, height){
    var elem = document.getElementById(id);
    if(elem.getAttribute('style')){
        elem.removeAttribute('style');
    } else {
        elem.style.width = weight + 'px';
        elem.style.height = height + 'px';
        elem.style.fontSize = '30px';
    }
}

var elems = document.getElementsByClassName('kaesten');
for(var i = 0; i < elems.length; i++){
    elems[i].onclick = function(){
        changeSize(this.id, 600, 600);
    }
}
<div id="box1" class="kaesten">test1</div>
<div id="box2" class="kaesten">test2</div>

更好的办法是简单地将元素作为引用传递给函数,而不是传递元素id 并不得不再次查询文档。

【讨论】:

  • 我想到了一个if函数,在上面编辑了。或者那不是好的编程?
  • 谢谢,它有效:) 但为什么我需要删除样式属性?
【解决方案2】:

在这种情况下,您可以做的最好的事情是使用 2 个 css 类并使用 javascript 切换它们。在每次点击时,您都可以检查它是否有某个类,然后切换它们

.normal{
    width:240px;
    height:300px;
    background-color:darkgrey;
    background-position:center;
    background-repeat:no-repeat;
    text-shadow:0px 0px 3px #000;
    border: 5px solid #F0F8ff;
    vertical-align:top;
    text-shadow: 3px 3px 4px #777;
    float:left;
    margin-left:30px;
}

.maximize{
    width:4800px;
    height:600px;
    font-size:30px;
    background-color:darkgrey;
    background-position:center;
    background-repeat:no-repeat;
    text-shadow:0px 0px 3px #000;
    border: 5px solid #F0F8ff;
    vertical-align:top;
    text-shadow: 3px 3px 4px #777;
    float:left;
    margin-left:30px;
}

你可以切换:

var class = document.getElementById("MyElement").className;
if(class === "normal"){
     document.getElementById("MyElement").className = "maximize";
} else {
     document.getElementById("MyElement").className = "normal";
}

【讨论】:

  • 我想用 JS 来做,类似的?函数 changeSize(id, weight, height) { with (document) { if (getElementById(id).style.width = '240px'){ getElementById(id).style.width = weight + 'px'; getElementById(id).style.height = height + 'px'; getElementById(id).style.fontSize = '30px'; } else { getElementById(id).style.width ='240px'; getElementById(id).style.height ='300px'; getElementById(id).style.fontSize = '18px'; } } }
【解决方案3】:

错别字

getElementById(id).style.fontSize = '30' +'px';
             ^

请检查它是否正常工作

function maxi(id, weight, height)
{
	with (document)
	{
		getElementById(id).style.width = weight + 'px';
		getElementById(id).style.height = height + 'px';
		getElementById(id).style.fontSize = '30' +'px';
		
	}
}
.kaesten{
	width:240px;
	height:300px;
	background-color:darkgrey;
	background-position:center;
	background-repeat:no-repeat;
	text-shadow:0px 0px 3px #000;
	border: 5px solid #F0F8ff;
	vertical-align:top;
	text-shadow: 3px 3px 4px #777;
	float:left;
	margin-left:30px;
}
<div id="box1" class="kaesten" onclick="maxi('box1', 600, 600); return false;">
test1

</div>

<div id="box2" class="kaesten">
test2
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-22
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多