gpt4 book ai didi

javascript - 检查javascript和警报中的背景颜色

转载 作者:太空宇宙 更新时间:2023-11-03 19:55:30 26 4
gpt4 key购买 nike

选项是蓝色的,在 javascript 的帮助下,我正在检查选项背景颜色是否为蓝色,如果不是蓝色则提前执行,否则会提示一些文本。

function click_option_A() {
var clr_1_chk = document.getElementById("1");
//alert(clr_1_chk.style.backgroundColor);
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_4_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else{
var clr = "1";
var confirm = window.confirm("Are you sure?");
if (confirm) {
window.document.getElementById(clr).style.backgroundColor ="yellow";
var right_answer = "B";
if(right_answer == "A"){
//correct_ans();
setTimeout(correct_ans,3000);
}
else{
setTimeout(wrong_ans,3000);
}
}
}
}

function click_option_B() {
var clr_1_chk = document.getElementById("1");
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_4_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else{
var clr = "2";
var confirm = window.confirm("Are you Sure?");
if (confirm) {
window.document.getElementById(clr).style.backgroundColor ="yellow";
var right_answer = "B";
if(right_answer == "B"){
//correct_ans();
setTimeout(correct_ans,3000);
}
else{
setTimeout(wrong_ans,3000);
}
}
}
}
function click_option_C() {
var clr_1_chk = document.getElementById("1");
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_4_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else{
var clr = "3";
var confirm = window.confirm("Are you Sure?");
if (confirm) {
window.document.getElementById(clr).style.backgroundColor ="yellow";
var right_answer = "B";
if(right_answer == "C"){
//correct_ans();
setTimeout(correct_ans,3000);
}
else{
setTimeout(wrong_ans,3000);
}
}
}
}
function click_option_D() {
var clr_1_chk = document.getElementById("1");
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_4_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else{
var clr = "4";
var confirm = window.confirm("Are you Sure?");
if (confirm) {
window.document.getElementById(clr).style.backgroundColor ="yellow";
var right_answer = "B";
if(right_answer == "D"){
//correct_ans();
setTimeout(correct_ans,3000);
}
else{
setTimeout(wrong_ans,3000);
}
}
}
}

function correct_ans(){
var clr_1_chk = document.getElementById("1");
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
var clr = "1";
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
var clr = "2";
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
var clr = "3";
}
else{
var clr = "4";
}
//alert('correct');
document.getElementById(clr).style.backgroundColor ="green";
}
function wrong_ans(){
var clr_1_chk = document.getElementById("1");
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
var clr = "1";
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
var clr = "2";
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
var clr = "3";
}
else{
var clr = "4";
}
document.getElementById(clr).style.backgroundColor ="red";
}
td{
background-color: blue;
color: white;
}
<div class="container">

<!--<form method="post" enctype="multipart/form-data" class="form-horizontal">-->

<table class="table table-bordered table-responsive">

<tr class="text-center">
<td colspan="2">Who was First Prime Minister of India?(15304)</td>
</tr>
<tr>
<td id="1" onclick="click_option_A()">A) M K Gandhi</td>
<td id="2" onclick="click_option_B()">B) Jawaharlal Nehru</td>
</tr>
<tr>
<td id="3" onclick="click_option_C()">C) Rajendra Prasad</td>
<td id="4" onclick="click_option_D()">D) Chandra Shekhar Azad</td>
</tr>

</table>

<!--</form>-->

</div>

我试过上面的代码,但它总是提醒我我已经锁定了答案,但我没有。

选项是蓝色的,在 javascript 的帮助下,我正在检查选项背景颜色是否为蓝色,如果不是蓝色则提前执行,否则会提示一些文本。

最佳答案

您没有获得 blue 样式的原因在这里,但是空字符串,因为 element.style仅返回内联样式,而不是 CSS 定义的样式。

如果您需要 CSS 样式,则必须使用 window.getComputedStyle(<element>) 获取计算样式.

更多信息请访问 MDN

在下面的代码片段中,我为 td 设置了内联样式.因此,您将获得 blue 的样式.

function click_option_A() {
var clr_1_chk = document.getElementById("1");
//alert(clr_1_chk.style.backgroundColor);
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_4_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else{
var clr = "1";
var confirm = window.confirm("Are you sure?");
if (confirm) {
window.document.getElementById(clr).style.backgroundColor ="yellow";
var right_answer = "B";
if(right_answer == "A"){
//correct_ans();
setTimeout(correct_ans,3000);
}
else{
setTimeout(wrong_ans,3000);
}
}
}
}

function click_option_B() {
var clr_1_chk = document.getElementById("1");
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_4_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else{
var clr = "2";
var confirm = window.confirm("Are you Sure?");
if (confirm) {
window.document.getElementById(clr).style.backgroundColor ="yellow";
var right_answer = "B";
if(right_answer == "B"){
//correct_ans();
setTimeout(correct_ans,3000);
}
else{
setTimeout(wrong_ans,3000);
}
}
}
}
function click_option_C() {
var clr_1_chk = document.getElementById("1");
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_4_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else{
var clr = "3";
var confirm = window.confirm("Are you Sure?");
if (confirm) {
window.document.getElementById(clr).style.backgroundColor ="yellow";
var right_answer = "B";
if(right_answer == "C"){
//correct_ans();
setTimeout(correct_ans,3000);
}
else{
setTimeout(wrong_ans,3000);
}
}
}
}
function click_option_D() {
var clr_1_chk = document.getElementById("1");
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else if(clr_4_chk.style.backgroundColor !== "blue"){
alert("You have already locked the answer!")
}
else{
var clr = "4";
var confirm = window.confirm("Are you Sure?");
if (confirm) {
window.document.getElementById(clr).style.backgroundColor ="yellow";
var right_answer = "B";
if(right_answer == "D"){
//correct_ans();
setTimeout(correct_ans,3000);
}
else{
setTimeout(wrong_ans,3000);
}
}
}
}

function correct_ans(){
var clr_1_chk = document.getElementById("1");
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
var clr = "1";
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
var clr = "2";
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
var clr = "3";
}
else{
var clr = "4";
}
//alert('correct');
document.getElementById(clr).style.backgroundColor ="green";
}
function wrong_ans(){
var clr_1_chk = document.getElementById("1");
var clr_2_chk = document.getElementById("2");
var clr_3_chk = document.getElementById("3");
var clr_4_chk = document.getElementById("4");
if(clr_1_chk.style.backgroundColor !== "blue"){
var clr = "1";
}
else if(clr_2_chk.style.backgroundColor !== "blue"){
var clr = "2";
}
else if(clr_3_chk.style.backgroundColor !== "blue"){
var clr = "3";
}
else{
var clr = "4";
}
document.getElementById(clr).style.backgroundColor ="red";
}
td{
background-color: blue;
color: white;
}
<div class="container">

<!--<form method="post" enctype="multipart/form-data" class="form-horizontal">-->

<table class="table table-bordered table-responsive">

<tr class="text-center">
<td colspan="2">Who was First Prime Minister of India?(15304)</td>
</tr>
<tr>
<td id="1" style="background-color: blue;color: white;" onclick="click_option_A()">A) M K Gandhi</td>
<td style="background-color: blue;color: white;" id="2" onclick="click_option_B()">B) Jawaharlal Nehru</td>
</tr>
<tr>
<td style="background-color: blue;color: white;" id="3" onclick="click_option_C()">C) Rajendra Prasad</td>
<td style="background-color: blue;color: white;" id="4" onclick="click_option_D()">D) Chandra Shekhar Azad</td>
</tr>

</table>

<!--</form>-->

</div>


我建议你保留一些 JS 对象来检查选项并提供正确答案。不建议引用对象样式来获取用户选项。

关于javascript - 检查javascript和警报中的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50021849/

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