0

我昨天问了一个问题,但它离题了,我已经解决了代码以通过悬停实现我想要的目标,但现在我希望每个被点击的 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";


    }
}}
4

3 回答 3

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>

于 2015-04-02T02:48:36.337 回答
0

问题是您使用的是“onmouseclick”而不是“onclick”

如何使用document.getElementsByClassName('content'), 而addEventListener()不是使用 'onX' 语法。我相信 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";
  });
}
于 2015-04-02T02:41:09.583 回答
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;}

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>

于 2015-04-02T02:54:46.593 回答