3 回答

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個贊
快速的答案是您可以簡單地將其附加到持有按鈕的元素:
$("#done").appendTo("#buttonGallery")
在這里,我對您的按鈕采取了一些自由,以減少工作量,將.on()
外部行為移動到.map
您從對象值生成按鈕的位置。如何獲取值可以通過多種方式完成,但使用數(shù)組,我只是在生成按鈕時將顏色添加到按鈕中,該data
屬性可用于顯示/隱藏和“修復(fù)”單擊時的顏色。為了好玩,我還展示了如何在第二次單擊時“切換”顏色。
由于您正在為您的按鈕庫生成一個div
,因此我將其添加到 HTML 中以稍微簡化代碼。
let myList = ["A", "B", "C", "D"];
let myColors = ["red", "green", "blue", "red"];
let gallery = $("#buttonGallery");// cache the reference to use elsewhere
myList.map(function(letter, index) {
let $button = $("<div class='buttons'><p></p></div>");
$button
.data("mycolor", myColors[index])
.attr("id", "button_" + letter)
.find("p")
.html(letter);
$button.appendTo(gallery);
});
gallery
.on("mouseenter", ".buttons", function() {
$(this).css("background", $(this).data("mycolor"));
})
.on("mouseleave", ".buttons", function() {
if (!$(this).is('.clicked')) {
$(this).css("background-color", "");
}
})
.on("click", ".buttons", function() {
let isClicked = $(this).is('.clicked');
let x = $(this).css('background-color');
if (x == $(this).data("mycolor")) {
$(this).css("background", $(this).data("mycolor"));
}
$(this).toggleClass('clicked', !isClicked);
});
$("#done")
.appendTo(gallery)
.on("click", clearColor);
function clearColor() {
$(".buttons")
.removeClass('clicked')
.css({
backgroundColor: ''
});
}
.buttons {
width: 150px;
height: 50px;
border: solid 2px black;
text-align: center;
color: black;
cursor: pointer;
background-color: white;
margin: 2px;
}
#buttonGallery {
margin: 10px;
padding: 10px;
border: solid 2px black;
width: 155px;
}
#done {
width: 150px;
height: 50px;
border: solid 2px black;
text-align: center;
color: black;
cursor: pointer;
background-color: white;
margin: 2px;
}
<html dir="ltr">
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="done">
<p>done</p>
</div>
<div id="buttonGallery"></div>
</body>
</html>

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個贊
<body>
<div id="buttonGallery"> <!-- add this line -->
<div id="done">
<p>done</p>
</div>
</div> <!-- add this line -->
<script type="text/javascript">
// let $buttons = $('<div id="buttonGallery">'); <-- remove this line
let $buttonGallery = $("#buttonGallery");
let myList = ["A", "B", "C", "D"];
let myColors = ["red", "green", "blue", "red"];
$buttonGallery.children(":not(#done)").remove(); // <-- add this to remove added button except done button.
myList.map(function(letter, index) {
let $button = $("<div></div>")
.addClass("buttons")
.attr("id", "button_" + letter)
.html("<p>" + letter + "</p>")
.on("mouseenter", function() {
$(this).css("background", myColors[index]);
})
.on("mouseleave", function() {
if (!$(this).hasClass('clicked')) {
$(this).css("background", "transparent");
}
})
.on("click", function() {
$(this).css("background", myColors[index]);
$(this).addClass('clicked');
})
$("#done").before($button); // <-- edit this line
});
// $("body").append($buttons); <-- remove this line
$("#done").on("click", clearColor);
function clearColor() {
$(".buttons").css({
backgroundColor: 'transparent'
});
$(".buttons").removeClass('clicked');
}
</script>
</body>
添加回答
舉報(bào)