我有5个元素,我选择其中一个,当我点击它将是红色但当我点击其他元素它也是红色

时间:2017-09-19 18:42:54

标签: javascript

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

  let touch = touches.first

    if let location = touch?.location(in: self) {
        let nodeArray = self.nodes(at: location)

        if nodeArray.first?.name == "playButton" {
            let transition = SKTransition.fade(withDuration: 0.5)
            let gameScene = GameScene(size: self.size)
            gameScene.scaleMode = SKSceneScaleMode.aspectFill
            self.view?.presentScene(gameScene, transition: transition)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

<body>
    <div class="container">
        <div class="item" id="item1" onclick="colorRed('item1')">click me to paint me red</div>
        <div class="item" id="item2" onclick="colorRed('item2')">click me to paint me red</div>
        <div class="item" id="item3" onclick="colorRed('item3')">click me to paint me red</div>
        <div class="item" id="item4" onclick="colorRed('item4')">click me to paint me red</div>
        <div class="item" id="item5" onclick="colorRed('item5')">click me to paint me red</div>
    </div>
    <script>
        function colorRed(param){
            document.getElementById(param).style.backgroundColor = "red";
        }
    </script>
</body>

如果我的问题是对的,那么这就是解决方案。您可以添加或替换图像代码,而不是红色。我创建了一个函数,其中一个参数只更新了被点击的元素。