gpt4 book ai didi

java - 在 js/php 范围内,什么是更好的选择/目标概念

转载 作者:行者123 更新时间:2023-12-02 09:27:14 24 4
gpt4 key购买 nike

我对这段代码的问题是在第二次单击时它取消选择所有内容并且不选择特定项目。我知道这是因为 item_'.$x.'Active 即使我有 selectorReset() 函数,它也不一定会重置每个单独 Activity 返回的值到0

我知道这是糟糕的编码,有人可以启发我更好的解决方案来实现这种功能吗? ^_^

示例:单击项目 0,然后单击项目 1(按预期工作正常)但如果我返回单击第 0 项,激活器将设置为 1,这意味着功能选择器中的 else 将使用重置功能激活,因此不会选择任何内容。问题:如何保持选中状态?解决方案:一种解决问题的简单方法哈哈,或者有人可以提出一个已知的概念吗?谢谢^_^

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="area">
<?php
$x=0;
while($x!=10){
echo'
<div id="item_'.$x.'" class="area__item" onclick="selector'.$x.'()">
'.$x.'
</div>
<script>
var item_'.$x.'Active = 0;
var item_'.$x.' = document.getElementById("item_'.$x.'");
function selector'.$x.'(){
if(item_'.$x.'Active == 0){
selectorReset();
item_'.$x.'.style.backgroundColor = "pink";
item_'.$x.'Active = 1;
}else{
selectorReset();
item_'.$x.'.style.backgroundColor = "";
item_'.$x.'Active = 0;
}
}
</script>
';
$x++;
}
?>
<script>
var item = document.getElementsByClassName("area__item");
function selectorReset(){
for(var i=0;i<item.length;i++){
item[i].style.backgroundColor="";
}
}
</script>
</div>
<style>
.area{
display:flex;
flex-wrap:wrap;
width:100%;
}
.area__item{
display:flex;
justify-content:center;
align-items:center;
height:5em;
width:5em;
border:1px solid black;
margin:1em;
cursor:pointer;
}
.area__item:hover{
background-color:pink;
}
</style>
</body>
</html>

最佳答案

试试这个代码

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.area{
display:flex;
flex-wrap:wrap;
width:100%;
}
.area__item{
display:flex;
justify-content:center;
align-items:center;
height:5em;
width:5em;
border:1px solid black;
margin:1em;
cursor:pointer;
}
.area__item:hover{
background-color:pink;
}
</style>
</head>
<body>
<div class="area">
<?php

$x = 0;
while($x!=10){
echo'
<div id="item_'.$x.'" class="area__item" onclick="selector('.$x.')">
'.$x.'
</div>';
$x++;
}
?>
</div>
<script>

function selector(item) {

// Get the total Class length

var classLength = document.getElementsByClassName("area__item");

// First check any of the classes holds the style attribute, then remove it

for(var i = 0; i < classLength.length;i++){
var element = document.getElementById('item_' + i);
if (element.hasAttribute("style")) {
element.style.backgroundColor = "";
}
}

// Get the selected element by argument
// Then add classes to the selectedElement

var selectedElement = document.getElementById('item_'+item);
selectedElement.style.backgroundColor = "pink";

}
</script>
</body>
</html>

关于java - 在 js/php 范围内,什么是更好的选择/目标概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58264015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com