我正在学习Jquery,我试图检查一个span
是否有某个类,这是问题的span
:
<span class="recaptcha-checkbox goog-inline-block
recaptcha-checkbox-unchecked recaptcha-checkbox-checked
rc-anchor-checkbox recaptcha-checkbox-expired" role="checkbox"
aria-checked="false" id="recaptcha-anchor" tabindex="0"
dir="ltr" aria-labelledby="recaptcha-anchor-label">
<div class="recaptcha-checkbox-border" role="presentation" style=""></div>
<div class="recaptcha-checkbox-borderAnimation" role="presentation" style=""></div>
<div class="recaptcha-checkbox-spinner" role="presentation"></div><div class="recaptcha-checkbox-spinnerAnimation" role="presentation"></div>
<div class="recaptcha-checkbox-checkmark" role="presentation"></div>
</span>
我们的想法是检查<span>
是否有类recaptcha-checkbox-checked
&#34;如果将类分配给元素,则.hasClass()方法将返回true,即使其他类也是如此。&#34;
所以我有以下内容:
captchaStatus: function () {
return $("#recaptcha-anchor").hasClass("recaptcha-checkbox-checked")
}
即使元素具有类,它也始终返回false。
编辑:
var AddressValidatorHelper = {
AddressValidator: function () {
if (!AddressValidatorHelper.changedAddress && AddressValidatorHelper.clickedVerify
&& AddressValidatorHelper.captchaStatus() && ValidatorControls.validate()) {
AddressValidatorHelper.noNormHide();
return true;
}
else {
if (!AddressValidatorHelper.alreadyAppended) {
AddressValidatorHelper.noNormAppend();
AddressValidatorHelper.noNormShow();
AddressValidatorHelper.alreadyAppended = true;
}
if (AddressValidatorHelper.changedAddress) {
AddressValidatorHelper.noNormShow();
}
if (AddressValidatorHelper.clickedVerify) {
AddressValidatorHelper.noNormHide();
}
return false;
}
},
noNormAppend: function () { $("#verDom").append("<label class='control-label' id='validarDom' style='color='#a94442'>Valide su domicilio</label>") },
noNormShow: function () { $("#validarDom").show() },
noNormHide: function () { $("#validarDom").hide() },
changedAddress: false,
clickedVerify: false,
alreadyAppended: false,
captchaStatus: function () {
return $("#recaptcha-anchor").hasClass("recaptcha-checkbox-checked")
}
}
所以我打电话给AddressValidatorHelper.captchaStatus()
答案 0 :(得分:-2)
请将您的js代码封装在jquery ready function
中$(function(){
//your has class checking code here
})