为什么querySelector(JavaScript)不能与Internet Explorer一起使用?

时间:2018-04-10 15:17:16

标签: javascript html

我在下面有以下代码,它应该将值从一个HTML页面传递到另一个HTML页面。该代码适用于Firefox,但它不适用于IE 11.你能告诉我为什么代码不工作?提前致谢

FORM.HTML

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>OrderUp</title>
</head>

<body>
     <form method="get" action="results.html" >  
    Name : <input name = "name" type= "text" id="name">
    <br>
    <input value = "Submit" type = "submit" >
 </form>
</body>
</Html>

RESULTS.HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Results</title>
</head>
<body>
    <h2> Your Form Has Been Submitted </h2>
<div class = "first"> Name: </div>
<script>
   let params = (new URL(document.location)).searchParams;
   let name = params.get("name");
   console.log(name) 
   document.querySelector('.first').innerText += name;
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您遇到的问题是您使用的是IE11不支持的功能(let,URL API)。看我的评论。以下适用于IE11。它基于this stackoverflow post。如果你想使用ES6语法,你可以试试ES6到ES5的转换器。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Results</title>
    <script>
var parseQueryString = function() {
    var str = window.location.search;
    var objURL = {};

    str.replace(
        new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
        function( $0, $1, $2, $3 ){
            objURL[ $1 ] = $3;
        }
    );
    return objURL;
};
    </script>
</head>
<body>
    <h2> Your Form Has Been Submitted </h2>
<div class = "first"> Name: </div>
<script>
   var params = parseQueryString();
   var name = params["name"];
   console.log(name) 
   document.querySelector('.first').innerText += name;
</script>
</body>
</html>