I have 2 cards
like the below.
<div class="card" id="b1">
<div class="card-header">
<div class="card-title">
Sample card 1
</div>
</div>
<div class="card-body">
<div class = "content1"> </div>
<div class = "content2"> </div>
<div class = "content3"> </div>
</div>
<button class="button1">0</button>
</div>
<!-- card 2 -->
<div class="card" id="b2">
<div class="card-header">
<div class="card-title">
Sample card 1
</div>
</div>
<div class="card-body">
<div class = "content1"> </div>
<div class = "content2"> </div>
<div class = "content3"> </div>
</div>
<button class="button1">0</button>
</div>
Js :
var btnVal = [];
function buttonData(card.id) {
btnVal[card.id] = ! btnVal[card.id] ? 1 : btnVal[card.id] == 2 ? 0 : btnVal[card.id] + 1;
switch(btnVal[card.id])
{
case 0:
return {
target: card.id,
content1 : "Nice to code using Js",
};
case 1:
return {
target: card.id,
content2 : "Nice to code using JQuery",
};
case 2:
return {
target: card.id,
content1 : "Hello",
content2 : "Greetings",
};
default:
return {};
}
}
function toaddtheText(card) {
Callback(buttonData(card) , function (data) {
bodyText(data)
});
}
function bodyText(data) {
if(data.content1)
{
var target = document.getElementById(data.target);
var content = target.getElementsByClassName('content1')[0];
content.innerHTML = data.content1;
}
if(data.content2)
{
var target = document.getElementById(data.target);
var content = target.getElementsByClassName('content2')[0];
content.innerHTML = data.content2;
}
}
$(document).on("click", ".button", function () {
buttonData(b1);
});
When I click the button
in a specific card for the first time, case 0
should be implemented, the next time, case1
and the next time case2
should be implemented.
But, If I click the button, it directly takes the case2
and not the case1
.
The problem is, the data
is taking only the content2
text for all the targets. The content1
text is ignored.
console.log(data) gives the following result
.
object {
target: b1,
content2: "Nice to code using JQuery",
},
object {
target: b2,
content2: "Nice to code using JQuery",
},
Could someone please help?
Many thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…