IndexOf给出“无法读取未定义的属性'indexOf'”

时间:2019-07-07 17:11:45

标签: javascript html

我正在尝试对搜索输入进行编码。

我从

中获取了值,并将其输入到JS代码中,并将其分配给了变量。

我收到一个错误:“无法读取null的属性'value'”。

我不知道该怎么解决。尝试了很多事情。

我尝试将数组变量更改为[“ document.getElementById(” text“)。value;”]。

function search() {

  var textArray = document.getElementById("text").value;
  var newArray = [textArray];
  var inputValue = document.getElementById("searchInt").value;
  var search = newArray.indexOf(inputValue);

  if (search == -1) {
    alert("No search results!");
  } else {
    document.getElementById("lorem").innerHTML = search;
  }

}
<!DOCTYPE html>

<html>

<head>
  <title>Practice</title>
  <script src="practice.js"></script>
  <style src="practice.css"></style>
</head>

<body>
  <div id="container">

    <input type="text" id="searchInt" placeholder="Search..." />
    <button type="button" id="searchBtn" onclick="search();">Search</button>

    <div class="container2">
      <p id="lorem">What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make
        a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
        and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      </p>
    </div>

  </div>
</body>

</html>

我需要在脚本之前加载DOM,但是除了在正文末尾写代码外,我真的不知道该怎么做。我不知道导入的JS文件的其他方式。

我也不想使用Jquery。

0 个答案:

没有答案