简体   繁体   English

将 css 应用于 google recaptcha 元素

[英]Apply css to google recaptcha element

I use Google reCAPTCHA in my forms with some validation hack, which allows stop submitting the form if reCAPTCHA is not checked.我在表单中使用 Google reCAPTCHA 并进行了一些验证技巧,如果未检查 reCAPTCHA,则允许停止提交表单。

The problem is, I want to apply some css rules (ie border-color) on a captcha element in iframe if user forgot to check it.问题是,如果用户忘记检查它,我想在 iframe 中的验证码元素上应用一些 css 规则(即边框颜色)。 I tried to remove all of the unnecessary parts of the code from my examples我试图从我的示例中删除所有不必要的代码部分

html form html表单

<form class="form-box" action="" method="POST">
    <label class="col-6" for="user-name">
        Name
        <input type="text" name="user-name" id="user-name"/>
    </label>

    <label class="col-6" for="user-phone">
        Phone
        <input type="text" name="user-phone" id="user-phone"/>
    </label>

    <div class="g-recaptcha" data-sitekey="[site-key here]"></div>

    <div class="col-6 wrap-btn">
        <input type="submit" name="submit" value="Step 2" class="btn blue">
    </div>
</form>

In my js code I tried to find necessary element with .contents() function在我的 js 代码中,我试图用 .contents() 函数找到必要的元素

$('.form-box').submit(function(e){
if (grecaptcha.getResponse() == ""){
    e.preventDefault();
    $('.g-recaptcha iframe').contents().find('.rc-anchor-light').css('border', '1px solid red');
}
else { $('.form-box').submit(); } });

So I got the error所以我得到了错误

The frame requesting access has a protocol of "http", the frame being accessed has a protocol of "https".请求访问的帧具有“http”协议,被访问的帧具有“https”协议。 Protocols must match.协议必须匹配。

I guess if I could send a request from https I'll get the same error or similar (because there are no ways to apply js code to cross-domain iframe elements, right?)我想如果我可以从 https 发送请求,我会得到相同或类似的错误(因为没有办法将 js 代码应用于跨域 iframe 元素,对吧?)

So, any ideas how to display a red border around reCAPTCHA block if user don't check it before submission?那么,如果用户在提交前不检查它,如何在 reCAPTCHA 块周围显示红色边框的任何想法?

The simplest way I can think of would be to put a border on the div that contains the iframe.我能想到的最简单的方法是在包含 iframe 的 div 上放置一个边框。 But, this might not necessarily be the same size as the iframe.但是,这可能不一定与 iframe 的大小相同。 However, there is a nested div inside of this that is the correct size.但是,其中有一个大小正确的嵌套 div。 So, the following code should do the trick:因此,以下代码应该可以解决问题:

$('.form-box').submit(function(e){
if (grecaptcha.getResponse() == ""){
    e.preventDefault();
    $('.g-recaptcha div div').css('border', '1px solid red');
}
else { $('.form-box').submit(); } });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM