如何使用子节点正确实现onclick以使div消失?

时间:2015-04-02 02:24:38

标签: javascript html transparency children child-nodes

昨天我问了一个问题,但这是偏离主题的,我已经解决了代码,通过悬停完成了我想要的目标,但现在我希望每个被点击的div变得透明。我知道的问题是我在Dreamweaver而不是phpstorm工作,我不确定我使用的命令是否有效。我的HTML在这里:

<!DOCTYPE html>
<html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div id="category">

            <div class="content">
            </div>

            <div class="content">
            </div>

            <div class="content">
            </div>

            <div class="content">
            </div>

            <div class="content">
            </div>

        </div>

    <style>
        div { background-color: springgreen; }
        div { width: 100px; }
        div { height: 100px; }
    </style>

</body>
<script src="js/main.js"></script>
</html>

和javascript:

/**
* Created by Mark M. on 3/28/2015.
*
*/

 var category = document.getElementById("category");
 for (var child = category.firstChild; child != null; child =    child.nextSibling) {
  if (child.nodeType == 1 && child.className == "content") {
    child.onmouseover = function() {
        this.style.width = "150px";
        this.style.height = "150px"
    };

    child.onmouseout = function() {
        this.style.width = "100px";
        this.style.height = "100px"
    };

    child.onmouseclick= function() {
        //disappear
        this.style.backgroundColor = "transparent";


    }
}}

3 个答案:

答案 0 :(得分:1)

对于您的问题,这应该是一个优雅的解决方案。单击“运行代码段”以检查:

var contentDivs = document.getElementsByClassName('content');

for(var i = 0; i < contentDivs.length; i++) {
    var div = contentDivs[i];
    div.onclick = function(){
        this.style.display = 'none';
    }
}
div {background-color: springgreen; width: 100px; height: 100px;}
<div id="category">
    <div class="content">Content 1</div>
    <div class="content">Content 2</div>
    <div class="content">Content 3</div>
    <div class="content">Content 4</div>
    <div class="content">Content 5</div>
</div>

答案 1 :(得分:0)

问题是你正在使用&#39; onmouseclick&#39;而不是&#39; onclick&#39;

如何使用document.getElementsByClassName('content')addEventListener()而不是使用&#39; onX&#39;句法。我相信addEventListener更符合标准,应该提供更好的浏览器兼容性。

https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

var content = document.getElementsByClassName('content');
console.log('content found', content);
for (var i = 0; i < content.length; i+=1){
  content[i].addEventListener("click", function(){
    this.style.backgroundColor = "transparent";
  });
}

答案 2 :(得分:0)

试试这个:

HTML

<div id="category">
    <div class="content"></div>
    <div class="content"></div>
    <div class="content"></div>
    <div class="content"></div>
    <div class="content"></div>
</div>

的JavaScript

var arrContent = document.querySelectorAll("#category div.content");
for(i =0; i< arrContent.length; i++){
    var child = arrContent[i];
    console.log(child)
    if (child.nodeType == 1 ) {
        child.onmouseover = function() {
            this.style.width = "150px";
            this.style.height = "150px"
        };

        child.onmouseout = function() {
            this.style.width = "100px";
            this.style.height = "100px"
        };

        child.onclick= function() {
            this.style.backgroundColor  = "transparent";
        }
    }
}`

样式表

div.content {background-color: springgreen;width: 100px;height: 100px;}

&#13;
&#13;
var arrContent = document.querySelectorAll("#category div.content");
for(i =0; i< arrContent.length; i++){
var child = arrContent[i];
console.log(child)
if (child.nodeType == 1 ) {
    child.onmouseover = function() {
        this.style.width = "150px";
        this.style.height = "150px"
    };

    child.onmouseout = function() {
        this.style.width = "100px";
        this.style.height = "100px"
    };

    child.onclick= function() {
        this.style.backgroundColor  = "transparent";
    }
}
}
&#13;
div.content {background-color: springgreen;width: 100px;height: 100px;}
&#13;
<div id="category">
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
</div>
&#13;
&#13;
&#13;