Javascript document.location如果页面具有具有特定值的类

Javascript document.location如果页面具有具有特定值的类,javascript,jquery,redirect,Javascript,Jquery,Redirect,我使用phpmailer,提交表单后需要重定向页面,并获得成功弹出窗口 当我有提交表单的成功结果时,弹出窗口中会显示发送的消息(JS添加类'msg'和值Sent) 我需要重定向并尝试: (function ($) { var text = document.getElementsByClassName("msg").innerHTML; if (text == "Sent") { window.location = "http://www.test.com"

我使用phpmailer,提交表单后需要重定向页面,并获得成功弹出窗口

当我有提交表单的成功结果时,弹出窗口中会显示发送的消息(JS添加类'msg'和值Sent)

我需要重定向并尝试:

(function ($) {
    var text = document.getElementsByClassName("msg").innerHTML;
    if (text == "Sent") {
        window.location = "http://www.test.com"
    }
})(jQuery);
但没有积极的结果。谁能帮帮我

UPD:这就回答了我的bootm应该是有效的,对不起,我想我没有清楚地描述我需要什么或者我有什么


我有一个PHPMailer,如果我成功提交表单,请返回到另一个PHPMailer JS脚本代码'MF000“这种编码和JS脚本在弹出窗口中添加带有“Sent”文本的“msg”类之后,我需要重定向到另一个页面,而不是这个弹出窗口,也许我可以使用“MF000”代码或其他东西的侦听器?

getElementsByClassName
返回一个数组,因此它没有
innerHTML
属性。您需要按索引访问结果数组。假设您需要第一个匹配的元素,那么这应该可以工作:

(function ($) {
    var text = document.getElementsByClassName("msg")[0].innerHTML.trim();
    if (text == "Sent") {
        window.location = "http://www.test.com"
    }
})(jQuery);
注意使用
trim()
删除可能影响比较检查的前导/尾随空格。或者,在使用jQuery时,您可以执行以下操作:

(function ($) {
    var text = $(".msg").text().trim();
    if (text == "Sent") {
        window.location = "http://www.test.com"
    }
})(jQuery);

另外,请确保在加载DOM后正在运行代码。单靠闭包无法完成这项工作。

GetElementsByCassName
返回一个数组,因此它没有
innerHTML
属性。您需要按索引访问结果数组。假设您需要第一个匹配的元素,那么这应该可以工作:

(function ($) {
    var text = document.getElementsByClassName("msg")[0].innerHTML.trim();
    if (text == "Sent") {
        window.location = "http://www.test.com"
    }
})(jQuery);
注意使用
trim()
删除可能影响比较检查的前导/尾随空格。或者,在使用jQuery时,您可以执行以下操作:

(function ($) {
    var text = $(".msg").text().trim();
    if (text == "Sent") {
        window.location = "http://www.test.com"
    }
})(jQuery);

另外,请确保在加载DOM后正在运行代码。单靠闭包是做不到这一点的。

console.log(text)
开始,既然您已经在使用jQuery,为什么不使用
$('.msg').html()
而不是
document.getElementsByClassName(“msg”).innerHTML
试试这个jsfiddle,从
console.log(text)
开始,既然您已经在使用jQuery,为什么不使用
$('.msg').html()
而不是
document.getElementsByClassName(“msg”).innerHTML试试这个JSFIDLE