在Javascript函数中难以理解

时间:2016-02-26 10:42:08

标签: javascript

我在Javascript中难以理解,我已经定义了这两个函数,但是浏览器consol说我有语法错误。这有什么问题,我该如何解决?

html和css代码:

<style> 
div.container {
    width: 300px;
    border: 1px solid;
}

div.box {
    width: 150px;
    border: 3px solid coral;
    float: left;
    padding: 10px;
}
</style>
</head>
<body>

<div class="container">
<div class="box" id="box1">This is BOX1.</div>
<div class="box" id="box2">This is BOX2.</div>
<div style="clear:both;"></div>
</div>

<p>Two 150 pixels boxes inside a 300 pixels container. It should fit nicely, but because of the borders and padding, the two boxes take up more space than 150 pixels each. This "problem" can be solved by setting the boxSizing property to "border-box".</p>




<button onclick="myFunction()">Try it</button>
<button onclick="removeHandler()"> Remove </button>

和Javascript代码:

<script>
function myFunction() {

    document.getElementById("box1").style.boxSizing = "border-box";
    document.getElementById("box2").style.boxSizing = "border-box";
}


function removeHandler() {
document.getElementById("box1").removeEventListener("style.boxSizing", removeHandler);
</script>

1 个答案:

答案 0 :(得分:1)

将您的上一个功能更改为:

function removeHandler() {
    document.getElementById("box1").removeEventListener("style.boxSizing", removeHandler);
}

您错过了最后一个括号,导致语法错误。