简体   繁体   English

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

[英]How would I properly implement onclick using childnodes to make a div disappear?

I asked a question yesterday but it was off-topic and I have worked around the code to accomplish the goal I wanted via hover but now I want each div that is clicked to become transparent. 昨天我问了一个问题,但这是题外话,我已经围绕代码进行工作,以通过悬停实现我想要的目标,但是现在我希望每个被单击的div都变得透明。 The problem that I know have is I am working in Dreamweaver instead of phpstorm and I am not sure if the command I used is valid. 我知道的问题是我在Dreamweaver中而不是phpstorm中工作,并且我不确定所使用的命令是否有效。 My html is here: 我的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>

and javascript: 和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";


    }
}}

This should be an elegant solution for your question. 对于您的问题,这应该是一个优雅的解决方案。 Click "Run Code Snippet" to check: 点击“运行代码段”进行检查:

 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> 

The problem is that you are using 'onmouseclick' instead of 'onclick' 问题是您使用的是“ onmouseclick”而不是“ onclick”

how about using document.getElementsByClassName('content') , and addEventListener() instead of using the 'onX' syntax. 如何使用document.getElementsByClassName('content')addEventListener()而不是使用'onX'语法。 I believe the addEventListener is more standards compliant and should provide better browser compatibility. 我相信addEventListener更符合标准,应该提供更好的浏览器兼容性。

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

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener 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";
  });
}

Try this : 尝试这个 :

HTML 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 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";
        }
    }
}`

StyleSheet 样式表

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

 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;} 
 <div id="category"> <div class="content"></div> <div class="content"></div> <div class="content"></div> <div class="content"></div> <div class="content"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM