我下面的代码包含2个列表,我目前想要完成的工作是将按钮放在Random Fruits list
内,如下图所示。
问题是我不确定为什么当我将按钮div放在边框内时才出现。有没有直接的方法可以做到这一点?任何帮助将不胜感激,谢谢!
var redpill = {};
var greenpill = {};
var randompill = {};
var key = "Red Fruits";
redpill[key] = ['Apple', 'Cherry', 'Strawberry', 'Pomegranate', 'Rassberry', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew'];
var key2 = "Green Fruits";
greenpill[key2] = ['Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew'];
var key3 = "Random Fruits";
randompill[key3] = ['Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew'];
function redraw() {
var combineString = '';
$.each(redpill[key], function(index) {
combineString += ('<div class="pilldiv redpill class">' + redpill[key][index] + '</div>');
});
$('.combineclass').html(combineString);
$.each(greenpill[key2], function(index) {
combineString += ('<div class="pilldiv greenpill class">' + greenpill[key2][index] + '</div>');
});
$('.combineclass').html(combineString);
var randomString = '';
$.each(randompill[key3], function(index) {
randomString += ('<div class="pilldiv randompill class">' + randompill[key3][index] + '</div>');
});
$('.randomclass').html(randomString);
}
function listener() {
$(document).ready(function() {
$(document).on("click", "#suggestid div", function() {
data = this.innerHTML;
$(".total_count_Green_Fruits").html(key2 + ': ' + greenpill[key2].length);
var element = $(this).detach();
$('#currentid').prepend('<div class="new-green-fruit pilldiv class ">' + element.html() + '</div>');
});
});
$('body').on('click', 'div.new-green-fruit', function() {
data2 = this.innerHTML;
$(this).detach();
var element2 = $(this).detach();
$('#suggestid').prepend('<div class="pilldiv randompill class" >' + element2.html() + '</div>');
});
}
redraw();
listener();
.pilldiv {
padding: 8px 15px;
text-align: center;
font-size: 15px;
border-radius: 25px;
color: Black;
margin: 2px;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.randompill:after {
content: "\002B";
float: left;
width: 16px;
}
.new-green-fruit:after {
content: "\292B";
float: left;
width: 16px;
}
.redpill {
background-color: Pink;
cursor: default;
}
.greenpill {
background-color: SpringGreen;
cursor: default;
}
.randompill {
background-color: LightBlue;
cursor: pointer;
}
.class {
font-family: Open Sans;
}
.center {
display: flex;
justify-content: center;
}
.wrappingflexbox {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.top {
margin-bottom: 20px
}
h3 {
font-weight: normal;
}
.panel {
display: table;
height: 100%;
width: 85%;
background-color: white;
border: 1px solid black;
margin-left: auto;
margin-right: auto;
}
.new-green-fruit {
background-color: LightBlue;
cursor: pointer;
}
.top {
margin-bottom: 30px;
}
#leftpanel {
float: left;
width: calc(50% - 5px);
}
#rightpanel {
float: right;
width: calc(50% - 5px);
}
.container {
width: 100%;
}
.suggestid {
border: 1px solid black;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
</style>
</head>
<body>
<div class="container">
<div id=leftpanel>
<h3 class="class center">Total Fruits</h3>
<div id="currentid" class="combineclass wrappingflexbox top"></div>
</div>
<div id="rightpanel">
<h3 class="class center">Random Fruits</h3>
<div id="suggestid" class="suggestid randomclass wrappingflexbox top"></div>
<div class="center">
<input class="modal-button explanation" style="white-space: pre-wrap;" type="button" data-toggle="modal" data-target="#myModal" value=" + View Interactive Skills Analysis Map!" />
</div>
</div>
</div>
</div>
</div>
<script>
</script>
</body>
</html>
答案 0 :(得分:3)
请从$('.randomclass').html(randomString);
更改为$('.randomclass').prepend(randomString);
,以防止覆盖按钮。
var redpill = {};
var greenpill = {};
var randompill = {};
var key = "Red Fruits";
redpill[key] = ['Apple', 'Cherry', 'Strawberry', 'Pomegranate', 'Rassberry', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew'];
var key2 = "Green Fruits";
greenpill[key2] = ['Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew'];
var key3 = "Random Fruits";
randompill[key3] = ['Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew'];
function redraw() {
var combineString = '';
$.each(redpill[key], function(index) {
combineString += ('<div class="pilldiv redpill class">' + redpill[key][index] + '</div>');
});
$('.combineclass').html(combineString);
$.each(greenpill[key2], function(index) {
combineString += ('<div class="pilldiv greenpill class">' + greenpill[key2][index] + '</div>');
});
$('.combineclass').html(combineString);
var randomString = '';
$.each(randompill[key3], function(index) {
randomString += ('<div class="pilldiv randompill class">' + randompill[key3][index] + '</div>');
});
$('.randomclass').prepend(randomString);
}
function listener() {
$(document).ready(function() {
$(document).on("click", "#suggestid .pilldiv", function() {
data = this.innerHTML;
$(".total_count_Green_Fruits").html(key2 + ': ' + greenpill[key2].length);
var element = $(this).detach();
$('#currentid').prepend('<div class="new-green-fruit pilldiv class ">' + element.html() + '</div>');
});
});
$('body').on('click', 'div.new-green-fruit', function() {
data2 = this.innerHTML;
$(this).detach();
var element2 = $(this).detach();
$('#suggestid').prepend('<div class="pilldiv randompill class" >' + element2.html() + '</div>');
});
}
redraw();
listener();
.pilldiv {
padding: 8px 15px;
text-align: center;
font-size: 15px;
border-radius: 25px;
color: Black;
margin: 2px;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.randompill:after {
content: "\002B";
float: left;
width: 16px;
}
.new-green-fruit:after {
content: "\292B";
float: left;
width: 16px;
}
.redpill {
background-color: Pink;
cursor: default;
}
.greenpill {
background-color: SpringGreen;
cursor: default;
}
.randompill {
background-color: LightBlue;
cursor: pointer;
}
.class {
font-family: Open Sans;
}
.center {
display: flex;
justify-content: center;
width: 100%;
}
.wrappingflexbox {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.top {
margin-bottom: 20px
}
h3 {
font-weight: normal;
}
.panel {
display: table;
height: 100%;
width: 85%;
background-color: white;
border: 1px solid black;
margin-left: auto;
margin-right: auto;
}
.new-green-fruit {
background-color: LightBlue;
cursor: pointer;
}
.top {
margin-bottom: 30px;
}
#leftpanel {
float: left;
width: calc(50% - 5px);
}
#rightpanel {
float: right;
width: calc(50% - 5px);
}
.container {
width: 100%;
}
.suggestid {
border: 1px solid black;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
</style>
</head>
<body>
<div class="container">
<div id=leftpanel>
<h3 class="class center">Total Fruits</h3>
<div id="currentid" class="combineclass wrappingflexbox top"></div>
</div>
<div id="rightpanel">
<h3 class="class center">Random Fruits</h3>
<div id="suggestid" class="suggestid randomclass wrappingflexbox top"> <div class="center"><input class="modal-button explanation" style="white-space: pre-wrap;" type="button" data-toggle="modal" data-target="#myModal" value=" + View Interactive Skills Analysis Map!" /></div>
</div>
</div>
</div>
</div>
</div>
<script>
</script>
</body>
</html>
答案 1 :(得分:0)
此按钮已被您编写的JavaScript代码覆盖。
var randomString = '';
$.each(randompill[key3], function(index) {
randomString += ('<div class="pilldiv randompill class">' + randompill[key3][index] + '</div>');
});
$('.randomclass').html(randomString);
将该按钮也添加到以下相同的js中
var randomString = '';
$.each(randompill[key3], function(index) {
randomString += ('<div class="pilldiv randompill class">' + randompill[key3][index] + '</div>');
});
randomString += ('<div class="center"> <input class="modal-button explanation" style="white-space: pre-wrap;" type="button" data-toggle="modal" data-target="#myModal" value=" + View Interactive Skills Analysis Map!" /></div>');
$('.randomclass').html(randomString);
尝试以下
var redpill = {};
var greenpill = {};
var randompill = {};
var key = "Red Fruits";
redpill[key] = ['Apple', 'Cherry', 'Strawberry', 'Pomegranate', 'Rassberry', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew'];
var key2 = "Green Fruits";
greenpill[key2] = ['Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew'];
var key3 = "Random Fruits";
randompill[key3] = ['Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew', 'Watermelon', 'Durian', 'Avacado', 'Lime', 'Honeydew'];
function redraw() {
var combineString = '';
$.each(redpill[key], function(index) {
combineString += ('<div class="pilldiv redpill class">' + redpill[key][index] + '</div>');
});
$('.combineclass').html(combineString);
$.each(greenpill[key2], function(index) {
combineString += ('<div class="pilldiv greenpill class">' + greenpill[key2][index] + '</div>');
});
$('.combineclass').html(combineString);
var randomString = '';
$.each(randompill[key3], function(index) {
randomString += ('<div class="pilldiv randompill class">' + randompill[key3][index] + '</div>');
});
randomString += ('<div class="center"> <input class="modal-button explanation" style="white-space: pre-wrap;" type="button" data-toggle="modal" data-target="#myModal" value=" + View Interactive Skills Analysis Map!" /></div>');
$('.randomclass').html(randomString);
}
function listener() {
$(document).ready(function() {
$(document).on("click", "#suggestid div", function() {
data = this.innerHTML;
$(".total_count_Green_Fruits").html(key2 + ': ' + greenpill[key2].length);
var element = $(this).detach();
$('#currentid').prepend('<div class="new-green-fruit pilldiv class ">' + element.html() + '</div>');
});
});
$('body').on('click', 'div.new-green-fruit', function() {
data2 = this.innerHTML;
$(this).detach();
var element2 = $(this).detach();
$('#suggestid').prepend('<div class="pilldiv randompill class" >' + element2.html() + '</div>');
});
}
redraw();
listener();
.pilldiv {
padding: 8px 15px;
text-align: center;
font-size: 15px;
border-radius: 25px;
color: Black;
margin: 2px;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.randompill:after {
content: "\002B";
float: left;
width: 16px;
}
.new-green-fruit:after {
content: "\292B";
float: left;
width: 16px;
}
.redpill {
background-color: Pink;
cursor: default;
}
.greenpill {
background-color: SpringGreen;
cursor: default;
}
.randompill {
background-color: LightBlue;
cursor: pointer;
}
.class {
font-family: Open Sans;
}
.center {
display: flex;
justify-content: center;
}
.wrappingflexbox {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.top {
margin-bottom: 20px
}
h3 {
font-weight: normal;
}
.panel {
display: table;
height: 100%;
width: 85%;
background-color: white;
border: 1px solid black;
margin-left: auto;
margin-right: auto;
}
.new-green-fruit {
background-color: LightBlue;
cursor: pointer;
}
.top {
margin-bottom: 30px;
}
#leftpanel {
float: left;
width: calc(50% - 5px);
}
#rightpanel {
float: right;
width: calc(50% - 5px);
}
.container {
width: 100%;
}
.suggestid {
border: 1px solid black;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
</style>
</head>
<body>
<div class="container">
<div id=leftpanel>
<h3 class="class center">Total Fruits</h3>
<div id="currentid" class="combineclass wrappingflexbox top"></div>
</div>
<div id="rightpanel">
<h3 class="class center">Random Fruits</h3>
<div id="suggestid" class="suggestid randomclass wrappingflexbox top"></div>
</div>
</div>
</div>
</div>
<script>
</script>
</body>
</html>