Jquery 无法读取replace';未定义';关于iframe元素
更新:发现在另一个局部视图中加载了reCaptcha脚本,这导致iFrame不存在,因为google API脚本不会在加载其他脚本之前加载 我正在实施reCaptcha v2的本地化。但是,我收到一个jQuery 3.3.1错误“无法读取未定义的属性'replace' 这个错误并不是每次都出现,我仍然很难找出这个问题。它发生在切换语言时,但即使我收到此错误,“iFrameGoogleRecaptcha”也不为空。我用谷歌搜索了这个问题,但大多数答案都指向我试图使用“replace”的不存在的元素,但我不明白如果元素从不为null,这怎么会是一个问题Jquery 无法读取replace';未定义';关于iframe元素,jquery,html,recaptcha,Jquery,Html,Recaptcha,更新:发现在另一个局部视图中加载了reCaptcha脚本,这导致iFrame不存在,因为google API脚本不会在加载其他脚本之前加载 我正在实施reCaptcha v2的本地化。但是,我收到一个jQuery 3.3.1错误“无法读取未定义的属性'replace' 这个错误并不是每次都出现,我仍然很难找出这个问题。它发生在切换语言时,但即使我收到此错误,“iFrameGoogleRecaptcha”也不为空。我用谷歌搜索了这个问题,但大多数答案都指向我试图使用“replace”的不存在的元素
有什么建议吗?你不应该只是检查
如果谷歌验证码
为空,你应该担心它的src
,因为这是你正在访问的东西。将if(iframeGoogleCaptcha!=null)
更改为if(iframeGoogleCaptcha.attr(“src”)!=undefined)
。这将使错误消失,而不会发生任何事情,但希望您能够更清楚地看到发生了什么。您不应该只检查如果框架GoogleCaptcha
为空,您应该担心它的src
,因为这是您正在访问的内容。将if(iframeGoogleCaptcha!=null)
更改为if(iframeGoogleCaptcha.attr(“src”)!=undefined)
。这将使错误消失,而不会发生任何事情,但希望您能够更清楚地看到发生了什么。因为iframeGoogleCaptxha.attr(“src”)未定义?也许可以尝试调试iframeGoogleCaptxha.attr(“src”)
,您会发现,因为iframeGoogleCaptxha.attr(“src”)未定义?请尝试调试iframeGoogleCaptxha.attr(“src”),您会发现
function setRecaptchaLanguage() {
var lang = 'hr';
switch (window.localStorage.getItem('activeCulture')) {
case 'hr':
lang = 'hr';
break;
case 'en-US-POSIX':
lang = 'en';
break;
}
if (window.localStorage.getItem('language') == 'ru-RU') {
lang = 'ru';
window.localStorage.setItem('language', null);
}
// Get GoogleCaptcha iframe
debugger
var iframeGoogleCaptcha = $('#captchaElement').find('iframe');
// For setting new language
if (iframeGoogleCaptcha != null)
iframeGoogleCaptcha.attr("src", iframeGoogleCaptcha.attr("src").replace(/hl=(.*?)&/, 'hl=' + lang + '&'));
}
$(document).ready(function () {
//set reCaptcha language
setRecaptchaLanguage();
});