这是我第一次在Stackoverflow上询问,
这是我的HTML代码,
<div id="accordion" style="height: auto;">
<h3>Question 1</h3>
<div>
<div id="q1">
Sending messages from another user's email address is called what?
<ul>
<li><button type="button" class="btn btn-secondary btn-sm optionbutton" data-optionId="option1">Spamming</button></li>
<li><button type="button" class="btn btn-secondary btn-sm optionbutton" data-optionId="option2">Trolling</button></li>
<li><button type="button" class="btn btn-secondary btn-sm optionbutton answer active clicked" data-optionId="option3">Spoofing</button></li>
<li><button type="button" class="btn btn-secondary btn-sm optionbutton" data-optionId="option4">Pharming</button></li>
</ul>
</div> <!-- q1 -->
</div>
<h3>Question 2</h3>
<div>
<div id="q2">
How many zebibytes are in a yobibyte?
<ul>
<li><button type="button" class="btn btn-secondary btn-sm optionbutton answer" data-optionId="option1">1,024</button></li>
<li><button type="button" class="btn btn-secondary btn-sm optionbutton" data-optionId="option2">1,048,576</button></li>
<li><button type="button" class="btn btn-secondary btn-sm optionbutton" data-optionId="option3">1,000,000</button></li>
<li><button type="button" class="btn btn-secondary btn-sm optionbutton active clicked" data-optionId="option4">1,000,000,000</button></li>
</ul>
</div><!-- q2 -->
</div>
&#13;
这是我的Jquery代码
var q1input = 0;
var q2input = 0;
var q3input = 0;
var q4input = 0;
var q5input = 0;
var optionele1 = $("#q1").find("button.active.clicked");
var optionele2 = $("#q2").find("button.active.clicked");
var optionele3 = $("#q3").find("button.active.clicked");
var optionele4 = $("#q4").find("button.active.clicked");
var optionele5 = $("#q5").find("button.active.clicked");
var answerele1 = $("#q1").find("button.answer");
var answerele2 = $("#q2").find("button.answer");
var answerele3 = $("#q3").find("button.answer");
var answerele4 = $("#q4").find("button.answer");
var answerele5 = $("#q5").find("button.answer");
if( optionele1.is(answerele1) ){ q1input = 100; }else{}
if( optionele2.is(answerele2) ){ q2input = 100; }else{}
if( optionele3.is(answerele3) ){ q3input = 100; }else{}
if( optionele4.is(answerele4) ){ q4input = 100; }else{}
if( optionele5.is(answerele5) ){ q5input = 100; }else{}
&#13;
问题是,我应该如何比较jquery中的两个元素?或者我的代码中有任何错误的语法?请帮忙。我被困在这里。
答案 0 :(得分:0)
您需要将jQuery对象中的第一个元素相互比较。例如:
if( $('.sel1')[0] == $('.sel2')[0] )
因此,您的代码应如下所示(请注意,也不需要else
块):
if( optionele1[0] == answerele1[0] ) { q1input = 100; }
// etc.